Example #1
0
 public function isValid($data = null, $entity = null)
 {
     if (!$this->security->checkToken()) {
         return false;
     }
     return parent::isValid($data, $entity);
 }
Example #2
0
 public function testFormValidator()
 {
     $this->specify("Form validators don't work", function () {
         //First element
         $telephone = new Text("telephone");
         $telephone->addValidator(new PresenceOf(array('message' => 'The telephone is required')));
         expect($telephone->getValidators())->count(1);
         $telephone->addValidators(array(new StringLength(array('min' => 5, 'messageMinimum' => 'The telephone is too short')), new Regex(array('pattern' => '/\\+44 [0-9]+ [0-9]+/', 'message' => 'The telephone has an invalid format'))));
         expect($telephone->getValidators())->count(3);
         //Second element
         $address = new Text('address');
         $address->addValidator(new PresenceOf(array('message' => 'The address is required')));
         expect($address->getValidators())->count(1);
         $form = new Form();
         $form->add($telephone);
         $form->add($address);
         expect($form->isValid([]))->false();
         $expectedMessages = Group::__set_state(array('_messages' => array(0 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The telephone is required', '_field' => 'telephone', '_code' => 0)), 1 => Message::__set_state(array('_type' => 'TooShort', '_message' => 'The telephone is too short', '_field' => 'telephone', '_code' => 0)), 2 => Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)), 3 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The address is required', '_field' => 'address', '_code' => 0)))));
         expect($form->getMessages())->equals($expectedMessages);
         expect($form->isValid(array('telephone' => '12345', 'address' => 'hello')))->false();
         $expectedMessages = Group::__set_state(array('_messages' => array(0 => Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)))));
         expect($form->getMessages())->equals($expectedMessages);
         expect($form->isValid(array('telephone' => '+44 124 82122', 'address' => 'hello')))->true();
     });
 }
Example #3
0
 public function testFormValidatorEntityBindSetters()
 {
     //Second element
     $address = new Text('address');
     $address->addValidator(new PresenceOf(array('message' => 'The address is required')));
     $telephone = new Text("telephone");
     $telephone->addValidator(new PresenceOf(array('message' => 'The telephone is required')));
     $entity = new ContactFormSettersGetters();
     $form = new Form();
     $form->add($address);
     $form->add($telephone);
     $form->bind(array('telephone' => '+44 123 45678', 'address' => 'hello'), $entity);
     $this->assertTrue($form->isValid());
     $this->assertEquals($entity->getTelephone(), '+44 123 45678');
     $this->assertEquals($entity->getAddress(), 'hello');
 }
Example #4
0
 /**
  * Rewrite validation class to handle afterValidateEvent
  * @param null $data
  * @param null $entity
  * @return bool
  */
 public function isValid($data = null, $entity = null)
 {
     return parent::isValid($data, $entity) && $this->afterValidation($data);
 }
Example #5
0
 public function testIssue1992()
 {
     $form = new \Phalcon\Forms\Form();
     $name = new \Phalcon\Forms\Element\Text("name");
     $name->addValidator(new StringLength(array('min' => 10, 'messageMinimum' => 'The name is too short')));
     $form->add($name);
     $form->appendMessage("name", new \Phalcon\Validation\Message('Must be not empty '));
     $messages = $form->getMessages();
     $this->assertEquals(count($messages), 1);
     $this->assertFalse($form->isValid(array('name' => 'phalcon')));
     $this->assertEquals(count($messages), 1);
     $form->appendMessages("name", array(new \Phalcon\Validation\Message('Must be not empty '), new \Phalcon\Validation\Message('Must be an email address')));
     $messages = $form->getMessages();
     $this->assertEquals(count($messages), 3);
 }
Example #6
0
 public function isValid($data = null, $entity = null)
 {
     $this->populate($data);
     $is_valid = parent::isValid($data, $entity);
     if (!$is_valid) {
         $this->has_errors = true;
     }
     return $is_valid;
 }
Example #7
0
 /**
  * Check isValid
  *
  * @param array $data
  * @param object $entity
  * @param bool $setAttributeErrorName
  * @return bool
  */
 public function isValid($data = null, $entity = null, $setAttributeErrorName = true)
 {
     if ($this->_titleColumn != '') {
         $data = $this->repaidSEOData($data);
     }
     //Supper isValid on parent
     $return = parent::isValid($data, $entity);
     $elements = $this->getElements();
     if (count($elements)) {
         foreach ($elements as $element) {
             $class = $element->getAttribute("class");
             $element->setAttribute("class", remove_multi_space($class . " has-success"));
         }
     }
     //Get message error
     $messages = $this->getMessages();
     if (!$return) {
         foreach ($messages as $message) {
             if (method_exists($message, "getField")) {
                 $error_element = $this->get($message->getField());
                 $validator = $error_element->getValidators();
                 if (is_array($validator) && isset($validator[0]) && method_exists($validator[0], 'getOption')) {
                     //Get class error
                     $class_error = $validator[0]->getOption('class_error');
                     if (!$class_error) {
                         $class_error = "has-error";
                     }
                     //Get current class name in field
                     $currentClass = str_replace("has-success", "", $error_element->getAttribute('class'));
                     //Add new class name error in field
                     $error_element->setAttribute('class', remove_multi_space($currentClass . " " . $class_error));
                     if ($setAttributeErrorName) {
                         //Get attribute name
                         $attribute_error_name = $validator[0]->getOption('attribute_error_name');
                         if (!$attribute_error_name) {
                             $attribute_error_name = "data-content";
                         }
                         //Get attribute content
                         $attribute_error_content = $validator[0]->getOption('attribute_error_content');
                         if ($attribute_error_content) {
                             $attribute_error_content = __($attribute_error_content);
                         } else {
                             $message = $validator[0]->getOption('message');
                             if ($message) {
                                 $attribute_error_content = __($message);
                             } else {
                                 $attribute_error_content = __("gb_form_this_field_is_required");
                             }
                         }
                         //Add error data content in field
                         $error_element->setAttribute($attribute_error_name, $attribute_error_content);
                     }
                     //Re add element error
                     $this->add($error_element);
                 }
             }
         }
     }
     //Return supper isValid
     return $return;
 }
Example #8
0
 /**
  * Tests clearing the Form Elements and using Form::isValid
  *
  * @issue  11978
  * @author Serghei Iakovlev <*****@*****.**>
  * @since  2016-10-01
  * @param  IntegrationTester $I
  */
 public function clearFormElementsAndUsingValidation(IntegrationTester $I)
 {
     $password = new Password('password', ['placeholder' => 'Insert your Password']);
     $password->addValidators([new PresenceOf(['message' => 'The field is required', 'cancelOnFail' => true]), new StringLength(['min' => 7, 'messageMinimum' => 'The text is too short'])]);
     $form = new Form();
     $form->add($password);
     $I->assertNull($form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $_POST = ['password' => 'secret'];
     $I->assertEquals('secret', $form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" value="secret" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $I->assertFalse($form->isValid($_POST));
     $actual = $form->getMessages();
     $expected = Group::__set_state(['_position' => 0, '_messages' => [Message::__set_state(['_type' => 'TooShort', '_message' => 'The text is too short', '_field' => 'password', '_code' => '0'])]]);
     $I->assertEquals($actual, $expected);
     $form->clear(['password']);
     $I->assertNull($form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $I->assertEquals(['password' => 'secret'], $_POST);
 }