• Resolved alexism59

    (@alexism59)


    Bonjour, je souhaite mettre en place une restriction d’âge pour mon formulaire de pré-inscription.

    J’ai suivi le tutoriel pour le mettre en place : https://wpforms.com/developers/how-to-provide-an-age-restriction-on-the-datepicker-form-field/ en remplaçant les ID par ce de mon formulaire.

    Mais ça ne fonctionne pas vraiment. J’ai bien un message qui indique qu’il y a une erreur, mais il ne précise pas laquelle (voir image).

    https://postimg.cc/mtkq66bC

    Voici le code utilisé:

    /**
    * Display an error message on submission of the form if the date doesn't fall within the guidelines.
    *
    * @link https://wpforms.com/developers/how-to-provide-an-age-restriction-on-the-datepicker-form-field/ *
    */

    function wpf_dev_process( $fields, $entry, $form_data ) {

    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #1183.
    if ( absint( $form_data[ 'id' ] ) !== 1183 ) {
    return $fields;
    }

    if ( isset( $fields[4][ 'value' ] ) && !empty( $fields[4][ 'value' ] ) ) {
    $timestamp = strtotime( $fields[4][ 'value' ] );
    if ($timestamp === false) {
    // Invalid date format
    wpforms()->process->errors[ $form_data[ 'id' ] ][ '4' ] = esc_html__( 'Invalid date format', 'plugin-domain' );
    } else {
    $birth_year = date('Y', $timestamp);
    $current_year = date('Y');
    $age = $current_year - $birth_year;

    if ($age < 18 || $age > 40) {
    // Show an error message at the top of the form and under the specific field
    wpforms()->process->errors[ $form_data[ 'id' ] ][ '4' ] = esc_html__( 'Inscription réservée aux 18–40 ans.', 'plugin-domain' );
    }
    }
    } else {
    // Date field is empty
    wpforms()->process->errors[ $form_data[ 'id' ] ][ '4' ] = esc_html__( 'Date field is required', 'plugin-domain' );
    }

    return $fields;
    }
    add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );

    Merci par avance

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Amjad Ali

    (@amjadali688)

    Hi @alexism59 ,

    We’d be happy to help!

    On checking your screenshot, I noticed that you’re using the Date field, which is available with our paid license. It seems that you are using the paid version of WPForms. If you have an active license subscription with us, could you please submit a support ticket through the WPForms account dashboard when you have a chance? 

    From there, our support team will be able to take a closer look at your request. 

    However, in the meanwhile, I would be happy to clarify your question.

    It looks like you’re currently using the Date Dropdown instead of the Date Picker field. As mentioned in this article (https://wpforms.com/developers/how-to-provide-an-age-restriction-on-the-datepicker-form-field/ ), the provided code is specifically designed for the Date Picker field type. To fix this, please change your field type to Date Picker in the form builder:

    1. Click on your date field in the form builder.
    2. Go to the Advanced tab under Field Options.
    3. Set the Date Type option to Date Picker.

    Once that’s updated, the code should work correctly, and the error message will appear under the date field as expected.

    Thanks!

    Thread Starter alexism59

    (@alexism59)

    Bonjour, merci pour votre réponse. FInalement, j’ai ajouté un code pour supprimer les années après 2007 et avant 1985 pour éviter le message d’erreur. Bonne soirée.

    Plugin Support Amjad Ali

    (@amjadali688)

    Hi @alexism59 ,

    You’re welcome!

    I’m glad to hear you resolved the issue by updating your code to adjust the year range.

    Please reach out if you have any questions.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problème restriction âge’ is closed to new replies.