ValidatorEmailList : validate a list of emails

0 gravatar By Grégoire Marchal - 01/25/2011

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

class ValidatorEmailList extends sfValidatorBase
{
  public function doClean($value)
  {
    $aValues = explode("\n", $value);
    $aValues = array_map('trim', $aValues);
    $aEmails = array();
    
    $oEmailValidator = new sfValidatorEmail();

    foreach ($aValues as $sEmail)
    {
      // ignore empty lines
      if ($sEmail != '')
      {
        // verify email syntax using sfValidatorEmail
        // sfValidatorError exception will be thrown if invalid
        $oEmailValidator->clean($sEmail);
        $aEmails[] = $sEmail;
      }
    }

    return $aEmails;
  }
}

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!

Back home

Comments (0)

Comment