Problème restriction âge
-
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).
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)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Problème restriction âge’ is closed to new replies.