Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken form getMessages #13295

Merged
merged 7 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-3.3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# [3.3.2](https://github.com/phalcon/cphalcon/releases/tag/v3.3.2) (2018-XX-XX)
- Fixed `Phalcon\Db\Dialect\Mysql::modifyColumn` to produce valid SQL for renaming the column [#13012](https://github.com/phalcon/cphalcon/issues/13012)
- Fixed `Phalcon\Forms\Form::getMessages` to return back previous behaviour: return array of messages with element name as key [#13294](https://github.com/phalcon/cphalcon/issues/13294)

# [3.3.1](https://github.com/phalcon/cphalcon/releases/tag/v3.3.1) (2018-01-08)
- Fixed a boolean logic error in the CSS minifier and a corresponding unit test so that whitespace is stripped [#13200](https://github.com/phalcon/cphalcon/pull/13200)
Expand Down
49 changes: 45 additions & 4 deletions phalcon/forms/form.zep
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,56 @@ class Form extends Injectable implements \Countable, \Iterator
}

/**
* Returns the messages generated in the validation
* Returns the messages generated in the validation.
*
* <code>
* if ($form->isValid($_POST) == false) {
* // Get messages separated by the item name
* // $messages is an array of Group object
* $messages = $form->getMessages(true);
*
* foreach ($messages as $message) {
* echo $message, "<br>";
* }
*
* // Default behavior.
* // $messages is a Group object
* $messages = $form->getMessages();
*
* foreach ($messages as $message) {
* echo $message, "<br>";
* }
* }
* </code>
*/
public function getMessages(boolean byItemName = false) -> <Group>
public function getMessages(boolean byItemName = false) -> <Group> | array
{
var messages;
var messages, messagesByItem, elementMessage, fieldName;

let messages = this->_messages;

if typeof messages == "object" && messages instanceof Group {
return messages;
/**
* @deprecated This part of code is for backward compatibility, it should be removed in next major version
*/
if unlikely byItemName {
let messagesByItem = [];
messages->rewind();

while messages->valid() {
let elementMessage = messages->current(),
fieldName = elementMessage->getField();

if !isset messagesByItem[fieldName] {
let messagesByItem[fieldName] = [];
}

let messagesByItem[fieldName][] = new Group([elementMessage]);
messages->next();
}
return messagesByItem;
}
return messages;
}

return new Group();
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,59 @@ public function testMergeValidators()
expect($form->get('address')->getMessages())->equals(Group::__set_state(['_messages' => []]));
});
}

/**
* Tests Form::getMessages(true)
* @author Mohamad Rostami <[email protected]>
* @issue 13294
* This should be removed in next major version
* We should not return multiple type of result in a single method! (form->getMessages(true) vs form->getMessages())
*/
public function testGetElementMessagesFromForm()
{
$this->specify('When form is not valid, iterate over messages by elements is not possible', 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->getMessages(true))->equals([
'telephone' => [
Group::__set_state([
'_messages' => [
Message::__set_state([
'_type' => 'Regex',
'_message' => 'The telephone has an invalid format',
'_field' => 'telephone',
'_code' => 0,
])
]
]),
Group::__set_state([
'_messages' => [
Message::__set_state([
'_type' => 'PresenceOf',
'_message' => 'The telephone is required',
'_field' => 'telephone',
'_code' => 0,
])
]
])
]
]);
});
}
}