【问题标题】:Validation Form in ZF2ZF2 中的验证表
【发布时间】:2013-12-19 06:15:51
【问题描述】:

如果电子邮件字段必须仅包含 6 到 12 个长度字符之间的有效电子邮件地址和密码,我如何验证我的电子邮件和密码字段中的输入数据? 我的表单代码:

namespace Notes\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputProviderInterface;
use Zend\InputFilter\InputFilter;
use Zend\Validator\ValidatorInterface;
use Zend\InputFilter\Factory as InputFactory;



class AuthorizationForm extends Form
{
    public function __construct($name = null)
     {
     parent::__construct('auth');
     $this->setAttribute('method', 'post');

     $this->add(array(
         'name' => 'email',
         'attributes' => array(
             'type'  => 'text',
             'style' => 'width: 250px;',
         ),
     ));

    $this->add(array(
        'name' => 'password',
        'attributes' => array(
            'type'  => 'password',
            'style' => 'width: 250px;',
         ),
    ));

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Login',
            'id' => 'submitbutton',
            'class' => 'fbutton green',
            ),
    ));

     $this->add(array(
         'type' => 'Zend\Form\Element\Checkbox',
         'name' => 'save_login',
         'options' => array(
             'label' => 'Remember me ',
             'checked_value' => '1',
         ),
     ));
   }
}

感谢您的回答!

【问题讨论】:

    标签: php validation filter zend-framework2


    【解决方案1】:

    将这些验证器添加到您的模型文件中:

      // For email validation
        'validators' => array(
            array('regex', true, array(
                'pattern'   => '/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i',
                'messages'  =>  'Your error message here...'))
        ),
    
      // For character length validation
        'validators' => array(
              array(
                  'name' => 'Between',
                  'options' => array(
                      'min' => 6,
                      'max' => 12,
                  ),
              ),
            ),
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      有用于电子邮件验证的内置验证器,如下所示,

      'validators' => array(                     
                  array('EmailAddress',true)                
                   )
      

      有关更多详细信息,请访问以下内容, http://zend-framework-community.634137.n4.nabble.com/ZF2-How-to-validate-a-password-and-email-in-zend-framework-2-td4657533.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-12
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        相关资源
        最近更新 更多