douyanjing8287
2014-08-19 00:17
浏览 56
已采纳

验证器不在Zend Framework 2中工作

The validator in my zend framework 2 are not working. Even as you can see required=>true works and shows me error if I pass an empty string. But the min and max length validations are not working and $form->isValid() just return true. Any help will be appreciated. I'm following this tutorial on Zend website. 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;
    }

} 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmeng7865 2014-08-19 09:33
    已采纳

    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.

    打赏 评论
  • duanri1985 2014-08-19 00:56

    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;
        }
    
    } 
    
    打赏 评论

相关推荐 更多相似问题