public function getMessages($byItemName = false) { $messages = array(); foreach (parent::getMessages(true) as $key => $message) { $messages[$key] = $message[0]->getMessage(); } return $messages; }
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(); }); }
/** * @param Form $form * @param string $messageType * @return \Phalcon\Http\ResponseInterface */ public function showInvalidMessagesAsJson(Form $form, $messageType = 'warning') { $messages = $form->getMessages(); $content = array(); foreach ($messages as $message) { $content[] = array('code' => 10001, 'message' => $message->getMessage(), 'message_human' => $this->getDI()->getTranslate()->query($message->getMessage())); } $this->response->setStatusCode(400, $this->recommendedReasonPhrases[400]); return $this->response->setJsonContent(array('errors' => $content)); }
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); }
/** * 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); }
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'))); }