public function testValidationSetLabels() { $_POST = array('email' => '', 'firstname' => ''); $validation = new Phalcon\Validation(); $validation->add('email', new PresenceOf(array('message' => 'The :field is required'))); $validation->add('email', new Email(array('message' => 'The :field must be email', 'label' => 'E-mail'))); $validation->add('firstname', new PresenceOf(array('message' => 'The :field is required'))); $validation->add('firstname', new StringLength(array('min' => 4, 'messageMinimum' => 'The :field is too short'))); $validation->setLabels(array('firstname' => 'First name')); $messages = $validation->validate($_POST); $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email', '_code' => '0')), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'Email', '_message' => 'The E-mail must be email', '_field' => 'email', '_code' => '0')), 2 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The First name is required', '_field' => 'firstname', '_code' => '0')), 3 => Phalcon\Validation\Message::__set_state(array('_type' => 'TooShort', '_message' => 'The First name is too short', '_field' => 'firstname', '_code' => '0'))))); $this->assertEquals($expectedMessages, $messages); }
public function testValidationCancelOnFail() { $validation = new Phalcon\Validation(); $validation->add('name', new PresenceOf(array('message' => 'The name is required')))->add('email', new PresenceOf(array('message' => 'The email is required', 'cancelOnFail' => true)))->add('login', new PresenceOf(array('message' => 'The login is required'))); $messages = $validation->validate($_POST); $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The name is required', '_field' => 'name')), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email'))))); $this->assertEquals($messages, $expectedMessages); }
public function testOptionAllowEmpty() { $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'Confirmation', '_message' => 'Field password must be the same as password2', '_field' => 'password', '_code' => '0'))))); // allowEmpty: true $validation = new Phalcon\Validation(); $validation->add('password', new Confirmation(array('allowEmpty' => true, 'with' => 'password2'))); $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0); $this->assertEquals(count($validation->validate(array('password' => null, 'password2' => 'test123'))), 0); // END // allowEmpty: false $validation = new Phalcon\Validation(); $validation->add('password', new Confirmation(array('allowEmpty' => false, 'with' => 'password2'))); $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0); $messages = $validation->validate(array('password' => null, 'password2' => 'test123')); $this->assertEquals(count($messages), 1); $this->assertEquals($messages, $expectedMessages); // END // allowEmpty: DEFAULT $validation = new Phalcon\Validation(); $validation->add('password', new Confirmation(array('with' => 'password2'))); $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0); $messages = $validation->validate(array('password' => null, 'password2' => 'test123')); $this->assertEquals(count($messages), 1); $this->assertEquals($messages, $expectedMessages); // END }
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, 'messageMinimum' => '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' => 'The telephone is too short', '_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'))); }