如果电子邮件字段必须仅包含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',
),
));
}
}
感谢您的回答!
答案 0 :(得分: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,
),
),
),
我希望这会有所帮助。
答案 1 :(得分:1)
内置的电子邮件验证验证器将是这样的,
'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