Ejemplo n.º 1
0
 /**
  * @param string $message
  * @return NotEmpty
  */
 public static function notEmptyValidator($message = null)
 {
     $validator = new NotEmpty();
     $validator->setOptions(['break_chain_on_failure' => true]);
     if ($message) {
         $validator->setMessage($message);
     } else {
         $validator->setMessage("Input masih kosong.");
     }
     return $validator;
 }
 public function __construct(EntityManager $em)
 {
     $input = new Input('prenom');
     $validator = new NotEmpty();
     $validator->setMessage('Le prénom est obligatoire', NotEmpty::IS_EMPTY);
     $input->getValidatorChain()->attach($validator);
     $filter = new StringTrim();
     $input->getFilterChain()->attach($filter);
     $this->add($input);
     $input = new Input('email');
     $input->setRequired(false);
     // TODO ne fonctionne pas pour l'UPDATE
     $validator = new NoObjectExists(array('object_repository' => $em->getRepository('AddressBook\\Entity\\Contact'), 'fields' => 'email'));
     $validator->setMessage("Un utilisateur utilise déjà cet email", NoObjectExists::ERROR_OBJECT_FOUND);
     $input->getValidatorChain()->attach($validator);
     if (class_exists('Zend\\Filter\\ToNull')) {
         $filter = new \Zend\Filter\ToNull();
     } else {
         if (class_exists('Zend\\Filter\\Null')) {
             $filter = new \Zend\Filter\Null();
         }
     }
     $input->getFilterChain()->attach($filter);
     $this->add($input);
     $input = new Input('societe');
     $input->setRequired(false);
     if (class_exists('Zend\\Filter\\ToNull')) {
         $filter = new \Zend\Filter\ToNull();
     } else {
         if (class_exists('Zend\\Filter\\Null')) {
             $filter = new \Zend\Filter\Null();
         }
     }
     $input->getFilterChain()->attach($filter);
     $this->add($input);
 }
Ejemplo n.º 3
0
 /**
  * @group ZF-11267
  * If we pass in a validator instance that has a preset custom message, this
  * message should be used.
  */
 function testIfCustomMessagesOnValidatorInstancesCanBeUsed()
 {
     // test with a Digits validator
     $data = array('field1' => 'invalid data');
     $customMessage = 'Hey, that\'s not a Digit!!!';
     $validator = new Validator\Digits();
     $validator->setMessage($customMessage, 'notDigits');
     $this->assertFalse($validator->isValid('foo'), 'standalone validator thinks \'foo\' is a valid digit');
     $messages = $validator->getMessages();
     $this->assertSame($messages['notDigits'], $customMessage, 'stanalone validator does not have custom message');
     $validators = array('field1' => $validator);
     $input = new InputFilter(null, $validators, $data);
     $this->assertFalse($input->isValid(), 'invalid input is valid');
     $messages = $input->getMessages();
     $this->assertSame($messages['field1']['notDigits'], $customMessage, 'The custom message is not used');
     // test with a NotEmpty validator
     $data = array('field1' => '');
     $customMessage = 'You should really supply a value...';
     $validator = new Validator\NotEmpty();
     $validator->setMessage($customMessage, 'isEmpty');
     $this->assertFalse($validator->isValid(''), 'standalone validator thinks \'\' is not empty');
     $messages = $validator->getMessages();
     $this->assertSame($messages['isEmpty'], $customMessage, 'stanalone NotEmpty validator does not have custom message');
     $validators = array('field1' => $validator);
     $input = new InputFilter(null, $validators, $data);
     $this->assertFalse($input->isValid(), 'invalid input is valid');
     $messages = $input->getMessages();
     $this->assertSame($messages['field1']['isEmpty'], $customMessage, 'For the NotEmpty validator the custom message is not used');
 }
Ejemplo n.º 4
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;
 }