[英]Validator not working in Zend Framework 2
The validator in my zend framework 2 are not working. 我的zend Framework 2中的验证器不起作用。 Even as you can see required=>true works and shows me error if I pass an empty string. 即使您看到required => true,也可以通过传递空字符串向我显示错误。 But the min and max length validations are not working and $form->isValid() just return true. 但是最小和最大长度验证无效,并且$ form-> isValid()仅返回true。 Any help will be appreciated. 任何帮助将不胜感激。 I'm following this tutorial on Zend website. 我正在Zend网站上关注本教程。 http://framework.zend.com/manual/2.3/en/user-guide/forms-and-actions.html http://framework.zend.com/manual/2.3/en/user-guide/forms-and-actions.html
<?php
namespace AdminAuthentication\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;
class Login implements InputFilterAwareInterface{
public $username;
public $password;
protected $inputFilter;
public function exchangeArray($data){
$this->username = isset($data['username']) ? $data['username'] : null;
$this->password = isset($data['password']) ? $data['password'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter){
throw new \Exception("Not Used!");
}
public function getInputFilter(){
if( ! $this->inputFilter ){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim' ),
),
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim' ),
),
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5
)
)
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
I think you missed something in the documentation tutorial . 我认为您错过了文档教程中的某些内容 。 Your code is wrong. 您的代码是错误的。
You have : 你有 :
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
)
You should have this : 你应该有这个:
'validators' => array (
array (
'name' => 'StringLength',
'options' => array (//<-----options array here
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100
)
)
)
The min
, max
and encoding
should be in the options
array. min
, max
和encoding
应该在options
数组中。
Well, above code was in documentation and not working. 好吧,以上代码在文档中,无法正常工作。 I don't know the reason but I've found the solution. 我不知道原因,但是我找到了解决方案。 For validator, replace 对于验证器,请更换
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5
)
)
with 与
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5
)
),
),
Here is the complete code. 这是完整的代码。
namespace AdminAuthentication\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;
use Zend\Validator\StringLength;
class Login implements InputFilterAwareInterface
{
public $username;
public $password;
protected $inputFilter;
public function exchangeArray($data)
{
$this->username = isset($data['username']) ? $data['username'] : null;
$this->password = isset($data['password']) ? $data['password'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not Used!");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5
)
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.