-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix form isValid method * fix zephir syntax erros * fix iterator - fix getMessageFor - unused getMessages byItemName * changelog + add unit tests * add test validation messages equals to form message * prevent exception in getMessagesFor * add test getMessagesFor for non existing element
- Loading branch information
1 parent
924326e
commit c505c68
Showing
3 changed files
with
188 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
use Phalcon\Validation\Validator\PresenceOf; | ||
use Phalcon\Validation\Validator\Regex; | ||
use Phalcon\Validation\Validator\StringLength; | ||
use Phalcon\Validation; | ||
|
||
/** | ||
* \Phalcon\Test\Unit\Forms\FormTest | ||
|
@@ -398,4 +399,145 @@ function () { | |
} | ||
); | ||
} | ||
|
||
/** | ||
* Tests Element::hasMessages() Element::getMessages() | ||
* | ||
* @author Mohamad Rostami <[email protected]> | ||
* @issue 11135, 3167 | ||
*/ | ||
public function testElementMessages() | ||
{ | ||
$this->specify('Element messages are empty if form validation fails', function () { | ||
// First element | ||
$telephone = new Text('telephone'); | ||
|
||
$telephone->addValidators([ | ||
new Regex([ | ||
'pattern' => '/\+44 [0-9]+ [0-9]+/', | ||
'message' => 'The telephone has an invalid format' | ||
]) | ||
]); | ||
|
||
// Second element | ||
$address = new Text('address'); | ||
$form = new Form(); | ||
|
||
$form->add($telephone); | ||
$form->add($address); | ||
|
||
expect($form->isValid(['telephone' => '12345', 'address' => 'hello']))->false(); | ||
expect($form->get('telephone')->hasMessages())->true(); | ||
expect($form->get('address')->hasMessages())->false(); | ||
|
||
expect($form->get('telephone')->getMessages())->equals( | ||
Group::__set_state([ | ||
'_messages' => [ | ||
Message::__set_state([ | ||
'_type' => 'Regex', | ||
'_message' => 'The telephone has an invalid format', | ||
'_field' => 'telephone', | ||
'_code' => 0, | ||
]) | ||
], | ||
]) | ||
); | ||
expect($form->get('telephone')->getMessages())->equals($form->getMessages()); | ||
expect($form->get('address')->getMessages())->equals(Group::__set_state(['_messages' => []])); | ||
expect($form->getMessagesFor('notelement'))->equals(Group::__set_state(['_messages' => []])); | ||
}); | ||
} | ||
|
||
/** | ||
* Tests Form::setValidation() | ||
* | ||
* @author Mohamad Rostami <[email protected]> | ||
* @issue 12465 | ||
*/ | ||
public function testCustomValidation() | ||
{ | ||
$this->specify('Injecting custom validation to form doesn\'t validate correctly', function () { | ||
// First element | ||
$telephone = new Text('telephone'); | ||
$customValidation = new Validation(); | ||
$customValidation->add('telephone', new Regex([ | ||
'pattern' => '/\+44 [0-9]+ [0-9]+/', | ||
'message' => 'The telephone has an invalid format' | ||
])); | ||
$form = new Form(); | ||
$address = new Text('address'); | ||
$form->add($telephone); | ||
$form->add($address); | ||
$form->setValidation($customValidation); | ||
expect($form->isValid(['telephone' => '12345', 'address' => 'hello']))->false(); | ||
expect($form->get('telephone')->hasMessages())->true(); | ||
expect($form->get('address')->hasMessages())->false(); | ||
expect($form->get('telephone')->getMessages())->equals( | ||
Group::__set_state([ | ||
'_messages' => [ | ||
Message::__set_state([ | ||
'_type' => 'Regex', | ||
'_message' => 'The telephone has an invalid format', | ||
'_field' => 'telephone', | ||
'_code' => 0, | ||
]) | ||
], | ||
]) | ||
); | ||
expect($form->get('telephone')->getMessages())->equals($form->getMessages()); | ||
expect($form->get('address')->getMessages())->equals(Group::__set_state(['_messages' => []])); | ||
}); | ||
} | ||
|
||
/** | ||
* Tests Form::isValid() | ||
* | ||
* @author Mohamad Rostami <[email protected]> | ||
* @issue 11500 | ||
*/ | ||
public function testMergeValidators() | ||
{ | ||
$this->specify('Injecting custom validation to form doesn\'t merge validators on isValid', function () { | ||
// First element | ||
$telephone = new Text('telephone'); | ||
$telephone->addValidators([ | ||
new PresenceOf([ | ||
'message' => 'The telephone is required' | ||
]) | ||
]); | ||
$customValidation = new Validation(); | ||
$customValidation->add('telephone', new Regex([ | ||
'pattern' => '/\+44 [0-9]+ [0-9]+/', | ||
'message' => 'The telephone has an invalid format' | ||
])); | ||
$form = new Form(); | ||
$address = new Text('address'); | ||
$form->add($telephone); | ||
$form->add($address); | ||
$form->setValidation($customValidation); | ||
expect($form->isValid(['address' => 'hello']))->false(); | ||
expect($form->get('telephone')->hasMessages())->true(); | ||
expect($form->get('address')->hasMessages())->false(); | ||
expect($form->get('telephone')->getMessages())->equals( | ||
Group::__set_state([ | ||
'_messages' => [ | ||
Message::__set_state([ | ||
'_type' => 'Regex', | ||
'_message' => 'The telephone has an invalid format', | ||
'_field' => 'telephone', | ||
'_code' => 0, | ||
]), | ||
Message::__set_state([ | ||
'_type' => 'PresenceOf', | ||
'_message' => 'The telephone is required', | ||
'_field' => 'telephone', | ||
'_code' => 0, | ||
]) | ||
], | ||
]) | ||
); | ||
expect($form->get('telephone')->getMessages())->equals($form->getMessages()); | ||
expect($form->get('address')->getMessages())->equals(Group::__set_state(['_messages' => []])); | ||
}); | ||
} | ||
} |