Ejemplo n.º 1
0
Archivo: Text.php Proyecto: t4web/base
 public function __construct($name = null, $min = 1, $max = 255)
 {
     $validatorStringLength = new Validator\StringLength(array('min' => $min, 'max' => $max));
     $validatorStringLength->setMessages(array(Validator\StringLength::INVALID => "Неверный формат поля", Validator\StringLength::TOO_LONG => "Количество символов в поле не может быть больше " . $max, Validator\StringLength::TOO_SHORT => "Количество символов в поле не может быть меньше " . $min));
     $this->getFilterChain()->attach(new FilterString());
     $this->getValidatorChain()->attach($validatorStringLength);
     parent::__construct($name);
 }
Ejemplo n.º 2
0
 /**
  * Ensures that we can set more than one message at a time,
  * by passing an array of key/message pairs.  Both messages
  * should be defined.
  *
  * @return void
  */
 public function testSetMessages()
 {
     $this->validator->setMessages(array(StringLength::TOO_LONG => 'Your value is too long', StringLength::TOO_SHORT => 'Your value is too short'));
     $this->assertFalse($this->validator->isValid('abcdefghij'));
     $messages = $this->validator->getMessages();
     $this->assertEquals('Your value is too long', current($messages));
     $this->assertFalse($this->validator->isValid('abc'));
     $messages = $this->validator->getMessages();
     $this->assertEquals('Your value is too short', current($messages));
 }
Ejemplo n.º 3
0
 public function getInputFilter()
 {
     if (!$this->inputFilter) {
         $inputFilter = new InputFilter();
         $factory = new InputFactory();
         $inputFilter->add($factory->createInput(array('name' => 'userID', 'required' => true, 'filters' => array(array('name' => 'Int')))));
         //用户名
         $usernameif = $factory->createInput(array('name' => 'username', 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim'))));
         $validatorChain = new ValidatorChain();
         $v = new StringLength(array('min' => 6, 'max' => 20, 'encoding' => 'UTF-8'));
         $v->setMessages(array(StringLength::TOO_SHORT => '用户名 \'%value%\' 太短了,至少需要6个字符', StringLength::TOO_LONG => '用户名 \'%value%\' 太长了,最多20个字符'));
         $validatorChain->attach($v);
         $v = new NotEmpty();
         $v->setMessage(array(NotEmpty::IS_EMPTY => '用户名不能为空'));
         $validatorChain->attach($v);
         $usernameif->setValidatorChain($validatorChain);
         $inputFilter->add($usernameif);
         //密码
         $psif = $factory->createInput(array('name' => 'upassword', 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim'))));
         $validatorChain = new ValidatorChain();
         $v = new StringLength(array('min' => 6, 'max' => 20, 'encoding' => 'UTF-8'));
         $v->setMessages(array(StringLength::TOO_SHORT => '密码\'%value%\' 太短了,至少需要6个字符', StringLength::TOO_LONG => '密码 \'%value%\' 太长了,最多20个字符'));
         $validatorChain->attach($v);
         $v = new NotEmpty();
         $v->setMessage(array(NotEmpty::IS_EMPTY => '密码不能为空'));
         $validatorChain->attach($v);
         $psif->setValidatorChain($validatorChain);
         $inputFilter->add($psif);
         /* 原始方法不能改变默认错误信息
                    $inputFilter->add($factory->createInput(array(
                        'name'     => 'upassword',
                        'required' => true,
                        'filters'  => array(
                            array('name' => 'StripTags'),
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name'    => 'StringLength',
                                'options' => array(
                                    'encoding' => 'UTF-8',
                                    'min'      => 6,
                                    'max'      => 20,
                                ),
                            ),
                        ),
                    )));
            */
         $this->inputFilter = $inputFilter;
     }
     return $this->inputFilter;
 }