Validator EmailAddress可以验证一组电子邮件

问题描述 投票:1回答:1

我有一个select2字段(标签)需要使用Zend框架2中的EmailAddress验证器进行验证。

select2字段的帖子是这样的:

'[email protected], [email protected]'

从这里开始,我的表单有一个输入过滤器,如下所示:

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',
                    ),
                ),
            ),
        );
    }

就像你看到的那样,我想验证我从表单中获得的所有邮件,所以我explode我的字符串成一个数组,以循环有效的每个电子邮件。

一个问题我不知道如何以及在何处验证此数组。在这个例子中,我得到错误'invalid type. String expected'意味着该验证器只接受一封电子邮件。

可以这样做吗?我可以将foreach放入选项回调中以验证我的每封电子邮件吗?

试过:

从我的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,
        )
    )
);

我们发誓:

<?=$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>

我试图将类型更改为Zend \ Form \ Email,即使这样......没有按预期工作

我得到了以前的错误信息:

- '[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

对不起,这是用法语,基本上它说字符串不匹配点原子模式,引用字符串模式和无效的电子邮件地址。但

[email protected]是第一封电子邮件,lol @ test.com是第二封。所以逗号是个问题

php validation email zend-framework2 jquery-select2
1个回答
3
投票

你为什么不接受OOP?为此创建新的验证器:

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;
    }
}

并在您的验证器规范中:

public function getInputFilterSpecification()
{
    return [
        // other fields
        'emailList'  => [
            'required'   => true,
            'validators' => [
                ['name'=> EmailAddressList::class],
            ],
        ],
    ];
    }
© www.soinside.com 2019 - 2024. All rights reserved.