排除以ZF2形式出现的价值

时间:2013-06-17 13:19:48

标签: zend-framework2 zend-form zend-form-element

如何排除$ _POST中收到的数据? 此表单用于修改用户数据: 表单必须验证电子邮件不存在,属于她已经记录的电子邮件:

public function __construct(Adapter $adapter)
{
    $no_record_exists = new NoRecordExists(array(
        'table'   => 'user',
        'field'   => 'email',
        'adapter' => $adapter,
        'exclude' => array(
            'field' => 'email',
            'value' => '$_POST['email']'
        )  
    ));       
    $this->add(array(
        'name' => 'email',
        'required' => true,
        'filters' => array(
            array(
                'name' => 'StripTags'
            ),
            array(
                'name' => 'StringTrim'
            )
        ),
        'validators' => array(
            $no_record_exists,
            array(
                'name' => 'EmailAddress',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min' => 5,
                    'max' => 48
                )
            ),
        )
    ));
}

}

2 个答案:

答案 0 :(得分:0)

这就是我包含排除过滤器的方法

    //include unique field validator
    $noRecordExist = new \Zend\Validator\Db\NoRecordExists(
        array(
            'table' => 'user',
            'field' => 'email',
            'adapter' => $adapter,
        )
    );
    $noRecordExist->setMessage('Email already exist');
`   $id = $_POST['id'];
    if($id > 0){
        $noRecordExist->setExclude('email != ' . $_POST['email']);
    }
    //you can add this validator to your filter chain

答案 1 :(得分:-1)