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(); }); }
public function testFormValidator() { //First element $telephone = new Text("telephone"); $telephone->addValidator(new PresenceOf(array('message' => 'The telephone is required'))); $this->assertEquals(count($telephone->getValidators()), 1); $telephone->addValidators(array(new StringLength(array('min' => 5, 'minimumMessage' => 'The telephone is too short')), new Regex(array('pattern' => '/\\+44 [0-9]+ [0-9]+/', 'message' => 'The telephone has an invalid format')))); $this->assertEquals(count($telephone->getValidators()), 3); //Second element $address = new Text('address'); $address->addValidator(new PresenceOf(array('message' => 'The address is required'))); $this->assertEquals(count($address->getValidators()), 1); $form = new Form(); $form->add($telephone); $form->add($address); $this->assertFalse($form->isValid(array())); $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The telephone is required', '_field' => 'telephone', '_code' => 0)), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'TooShort', '_message' => 'Value of field \'telephone\' is less than the minimum 5 characters', '_field' => 'telephone', '_code' => 0)), 2 => Phalcon\Validation\Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)), 3 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The address is required', '_field' => 'address', '_code' => 0))))); $this->assertEquals($form->getMessages(), $expectedMessages); $this->assertFalse($form->isValid(array('telephone' => '12345', 'address' => 'hello'))); $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0))))); $this->assertEquals($form->getMessages(), $expectedMessages); $this->assertTrue($form->isValid(array('telephone' => '+44 124 82122', 'address' => 'hello'))); }