Checking the submitted values

The verifier() function is used to analyse the values posted and return errors that may exist concerning the data entered. To do this, the function returns a paired "field" / "error message" array of the offending fields, and also possibly a general message for the whole of the form using the "message_erreur" key.

The form processing function will be called on ONLY if the table returned is empty. If it is not, the form is redisplayed with the various error messages that have been passed.

  1. function formulaire_nom_verifier_dist() {
  2. $erreurs = array();
  3. foreach(array('titre','texte') as $champ) {
  4. if (!_request($champ)) {
  5. $erreurs[$champ] = "This data is mandatory!";
  6. }
  7. }
  8. if (count($erreurs)) {
  9. $erreurs['message_erreur'] = "An error occured in your data entry";
  10. }
  11. return $erreurs;
  12. }

Download

The formulaire_verifier pipeline is used to supplement the list of returned errors.

Example

The "Amis" (Friends) plugin has a form for inviting people to become your friend! The verifier() function checks that the mail address of the person being invited is correctly formatted:

  1. function formulaires_inviter_ami_verifier_dist(){
  2.  
  3. $erreurs = array();
  4. foreach(array('email') as $obli)
  5. if (!_request($obli))
  6. $erreurs[$obli] = (isset($erreurs[$obli])?$erreurs[$obli]:'') . _T('formulaires:info_obligatoire_rappel');
  7.  
  8. if ($e=_request('email')){
  9. if (!email_valide($e))
  10. $erreurs['email'] = (isset($erreurs['email'])?$erreurs['email']:'') . _T('formulaires:email_invalide');
  11. }
  12.  
  13. return $erreurs;
  14. }

Download

Author Mark Baber Published : Updated : 09/05/10

Translations : English, français