Comment valider un textfield generer par #ajax

Information importante

En raison d'un grand nombre d'inscriptions de spammers sur notre site, polluant sans relache notre forum, nous suspendons la création de compte via le formulaire de "sign up".

Il est néanmoins toujours possible de devenir adhérent•e en faisant la demande sur cette page, rubrique "Inscription" : https://www.drupal.fr/contact


De plus, le forum est désormais "interdit en écriture". Il n'est plus autorisé d'y écrire un sujet/billet/commentaire.

Pour contacter la communauté, merci de rejoindre le slack "drupalfrance".

Si vous voulez contacter le bureau de l'association, utilisez le formulaire disponible ici, ou envoyez-nous un DM sur twitter.

Salut, Je suis en train d'apprendre comment d'implementer un formulaire contenant l'attribut #ajax. J'ai utiliser le code se trouvant dans ajax_example.module mais quand j'essaye de valider la valeur entree dans le textfield 'first_name' par exemple sa ne marche pas!Peut etre la methode que j'utilise n'est pas correct ou contient des erreurs. Pouvez vous m'adider sur ca s'il vous plait!merci d'avance pour votre temps et aide. Voici mon code:

<?php
/<strong>
 *
Show/hide textfields based on AJAX-enabled checkbox clicks.
 */
function
ajax_example_autotextfields($form, &$form_state) {

 
$form['ask_first_name'] = array(
   
'#type' => 'checkbox',
   
'#title' => t('Ask me my first name'),
   
'#ajax' => array(
     
'callback' => 'ajax_example_autotextfields_callback',
     
'wrapper' => 'textfields',
     
'effect' => 'fade',
    )
  );
 
$form['ask_last_name'] = array(
  
'#type' => 'checkbox',
  
'#title' => t('Ask me my last name'),
   
'#ajax' => array(
     
'callback' => 'ajax_example_autotextfields_callback',
     
'wrapper' => 'textfields',
     
'effect' => 'fade',
    ),
  );

 
$form['textfields'] = array(
   
'#title' => t("Generated text fields for first and last name"),
   
'#prefix' => '<div id="textfields">',
   
'#suffix' => '</div>',
   
'#type' => 'fieldset',
   
'#description' => t('This is where we put automatically generated textfields'),
  );

 
// Since checkboxes return TRUE or FALSE, we have to check that
  // $form_state has been filled as well as what it contains.
 
if (!empty($form_state['values']['ask_first_name']) && $form_state['values']['ask_first_name']) {
   
$form['textfields']['first_name'] = array(
     
'#type' => 'textfield',
     
'#title' => t('First Name'),
    );
  }
  if (!empty(
$form_state['values']['ask_last_name']) && $form_state['values']['ask_last_name']) {
   
$form['textfields']['last_name'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Last Name'),
    );
  }

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Click Me'),
  );

  return
$form;
}

/</
strong>
 *
Selects the piece of the form we want to use as replacement text and returns
 
* it as a form (renderable array).
 *
 * @return
renderable array (the textfields element)
 */
function
ajax_example_autotextfields_callback($form, $form_state) {
  return
$form['textfields'];
}

/<
strong>
 *
implementing hoo_validate for the form
 
*/
function
ajax_example_autotextfields_validate($form, &$form_state) {
 
//$name = $form_state['values']['textfields']['first_name'];
 
if ($form_state['values']['textfields']['first_name'] == 'King Kong') {
   
form_set_error('first_name', t('Ton prenom n''est pas valide'));
  }
}

/</
strong>
 *
hook_submit
 
*/
function
ajax_example_autotextfields_submit($form, &$form_state) {
 
$name = $form_state['values']['textfields']['first_name'];
 
drupal_set_message(t('Tu as un bon prenom, %name', array('%name' => $name)));
}
?>
Forum : 
Version de Drupal :