Symfonic Let’s talk about Symfony stuff!

25Jan/110

ValidatorEmailList : validate a list of emails

Here is a short and simple validator that validates a list of email addresses from a "textarea" field.

  1. class ValidatorEmailList extends sfValidatorBase
  2. {
  3.   public function doClean($value)
  4.   {
  5.     $aValues = explode("\n", $value);
  6.     $aValues = array_map('trim', $aValues);
  7.     $aEmails = array();
  8.    
  9.     $oEmailValidator = new sfValidatorEmail();
  10.  
  11.     foreach ($aValues as $sEmail)
  12.     {
  13.       // ignore empty lines
  14.       if ($sEmail != '')
  15.       {
  16.         // verify email syntax using sfValidatorEmail
  17.         // sfValidatorError exception will be thrown if invalid
  18.         $oEmailValidator->clean($sEmail);
  19.         $aEmails[] = $sEmail;
  20.       }
  21.     }
  22.  
  23.     return $aEmails;
  24.   }
  25. }

By the way, we could improve it by passing the validator (here "new sfValidatorEmail()") as an option of ValidatorEmailList, then we could validate lists of whatever actually... Well, you can do it!

About Grégoire Marchal

Another Symfony developer...
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.