Drupal and Zend, form validate :)
Submitted by Sir Squall on Mon, 04/19/2010 - 14:02
Ce qui est cool sur drupal c'est que pour crée des formulaires c'est super simple, et c'est qui est cool sur zend ce que de valider un formulaire c'est super simple, alros ce qui est classe c'est quand on a drupal et zend bah on peut faire des truc qui on de la gueule :
/** * form */ function myzend_form($form_state){ $form['mail'] = array( '#type' => 'textfield', '#title' => t('mail'), '#default_value' => variable_get('mail', null), '#description' => t('Entrer votre adresse mail'), '#zend' => 'Zend_Validate_EmailAddress', ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); return $form; } /** * Validate */ function myzend_form_validate($form,&$form_state){ //automated validation zend foreach($form as $key => $value){ //check if have a zend validation if(!is_array($value) || !isset($value['#zend'])){ continue; } //get the value $val = $form_state['values'][$key]; //load the class zend if(zend_initialize($value['#zend']) && zend_initialize('Zend_Validate_Abstract')){ //initialize the validator $validator = new $value['#zend'](); //check if is the good instance if(!$validator instanceof Zend_Validate_Abstract){ form_set_error($key,t('Erreur')); } //validate if (!$validator->isValid($val)) { foreach ($validator->getMessages() as $messageId => $message) { form_set_error($key, t("Validation failure '$messageId': $message")); } } } } }







Recent comments
2 weeks 2 days ago
2 weeks 2 days ago
25 weeks 3 days ago
31 weeks 6 days ago
32 weeks 1 day ago
35 weeks 6 days ago
36 weeks 1 hour ago
36 weeks 1 day ago
40 weeks 6 days ago
42 weeks 4 days ago