2
votes

I have a select2 field (tags) wich need to be validated with EmailAddress validator from Zend framework 2.

the post from select2 field is like this :

'[email protected], [email protected]'

From here, my form has an input filter wich looks like this :

public function getInputFilterSpecification()
    {
        return array(
            'name'  => array(
                'required'   => true,
                'filters'    => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
            ),
            'emailList'  => array(
                'required'   => true,
                'filters'    => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array(
                        'name' => 'Callback',
                        'options' => array(
                            'callback' => function($value) {
                                if (is_string($value)) {
                                    $value = explode(',', $value);
                                }
                                return $value;
                            },
                        ),
                    )
                ),
                'validators' => array(
                    array(
                        'name' => 'EmailAddress',
                    ),
                ),
            ),
        );
    }

Like you see, i wanna validate all mails i've got from the form, so i explode my string into an array for valid each email in a loop.

One problem i don't know how and where to validate this array. In this example i get the error 'invalid type. String expected' means that only one email is accepted for this validator.

Is possible to do this ? Can i put a foreach into an option callback for validate each of my emails ?

Tried :

From my Fieldset :

$this->add(
    array(
        'name'    => 'emailList',
        'type' => 'Zend\Form\Element\Email',
        'options' => array(
            'label' => 'email_list',
            'label_attributes' => array(
                'class'  => 'control-label col-xs-5'
            ),
        ),
        'attributes' => array(
            'class' => 'form-control select-tags col-xs-5 nopadding',
            'multiple' => true,
        )
    )
);

My vue :

<?=$this->formEmail($reporting->get('emailList'));?>
<p class="col-xs-4 help-block">
  <?php 
    if ($this->formElementErrors($reporting->get('emailList'))) {
      echo $this->formElementErrors()
        ->setMessageOpenFormat('- ')
        ->setMessageSeparatorString('<br/>- ')
        ->setMessageCloseString('')
        ->render($reporting->get('emailList'));
    }
  ?>
</p>

I tried to change the type to Zend\Form\Email and even with that...nothing works as expected

And i get the same error message i had before :

- '[email protected],lol' ne correspond pas au format dot-atom
- '[email protected],lol' ne correspond pas au format quoted-string
- '[email protected],lol' n'est pas une partie locale valide pour l'adresse email

Sorry it's in french, basically it says that the string doesn't match dot-atom pattern, quoted-string pattern and not valid email address. But

[email protected] was the first email, and [email protected] was the second. so the comma is a problem

1
Imo use Zend\Form\Element\Email with multiple true. See framework.zend.com/manual/current/en/modules/…venca
@venca I edit my post with your idea, nice hint btw, but it seems i do it wrong or it's not working as expected.Greco Jonathan

1 Answers

4
votes

Why are you not embracing OOP? Create new validator for this:

namespace YourApp\Validator;

use Zend\Validator\EmailAddress;

class EmailAddressList extends EmailAddress
{
    public function isValid($value)
    {
        $emails = array_map('trim', explode(',', $value));
        foreach ($emails as $email) {
            if (!parent::isValid($email)) {
                return false;
            }
        }

        return true;
    }
}

and in your validator spec:

public function getInputFilterSpecification()
{
    return [
        // other fields
        'emailList'  => [
            'required'   => true,
            'validators' => [
                ['name'=> EmailAddressList::class],
            ],
        ],
    ];
    }