Submitted by nikatdrup on
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)));
}
?>
la solution ete d’ajouter
Permalien Soumis par nikatdrup le 18 Décembre, 2012 - 16:53
la solution ete d'ajouter '#tree' => TRUE a textfields