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

Form validation some fixes #12466

Merged
merged 7 commits into from
Dec 20, 2016
Merged

Form validation some fixes #12466

merged 7 commits into from
Dec 20, 2016

Conversation

mbrostami
Copy link
Contributor

@mbrostami mbrostami commented Dec 9, 2016

Hello!

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the Contributing Guidelines?
  • I have checked that another pull request for this purpose does not exist.
  • I wrote some tests for this PR.

Small description of change:
setValidation for form is not work correctly as I explained at #12465.
Elements messages doesn't set if custom validation fails.

Thanks


if !(validation instanceof Validation) {
// Create an implicit validation
validation = new Validation();
Copy link
Contributor

@sergeyklay sergeyklay Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The let statement is used to mutate variables, properties and arrays. Variables are by default immutable and this instruction makes them mutable:

let validation = new Validation();


let validation = this->getValidation();

if !(validation instanceof Validation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here does not need braces

for elementMessage in messages {
this->get(elementMessage->getField())->appendMessage(elementMessage);
}
validationStatus = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let validationStatus = false;

@sergeyklay sergeyklay added this to the 3.0.3 milestone Dec 9, 2016

if !(validation instanceof Validation) {
// Create an implicit validation
let validation = new Validation();
Copy link
Contributor

@sergeyklay sergeyklay Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think I misled you. The exclamation point has a higher priority. So you should use:

if !(validation instanceof Validation) {

But actually I think that more flexibility was to use the interface but not concrete class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! You're right. Thanks for comments.

@mbrostami mbrostami force-pushed the validation-fix branch 2 times, most recently from 2919cda to ce6440b Compare December 9, 2016 20:18
if typeof messages != "array" {
return new Group();
}
return messages;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, returning multiple type of value is not a good idea (see line 378 old - return type).

@sergeyklay
Copy link
Contributor

sergeyklay commented Dec 10, 2016

@mbrostami Could you please split this PR into parts: one that can be merged to the 3.0.x branch without backward compatibility breaking and another one that need to be sent to the 4.0.x branch. Also I would like to see a more detailed explanation of your changes.

Thanks

@@ -712,7 +684,11 @@ class Form extends Injectable implements \Countable, \Iterator
public function rewind() -> void
{
let this->_position = 0;
let this->_elementsIndexed = array_values(this->_elements);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you create a form without any element, and iterate that, php warning will be shown, because this->_elements is null. I added some code to prevent that. https://github.com/phalcon/cphalcon/blob/3.0.x/tests/unit/Forms/FormTest.php#L77

@mbrostami
Copy link
Contributor Author

I think using _messages as a single type (Group|null) would be better, and will avoid of complexity.
In old code, you can see _messages as an array and somewhere else as a Group object!
Other issue was about a bug for getMessagesFor method, which didn't return correct value(#11135). So inside isValid method, after validation, setting error messages to each element fixes this issue and also after that you can get each element messages from this->get(elementName)->getMessages() and doesn't need to iterate _messages.(would be cleaner code I think).
Other issue is injecting CustomValidation to a Form by setValidation method, it didn't work, now is fixed. (#12465 I don't know if it was a bug or not!).

Copy link
Contributor

@sergeyklay sergeyklay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cc: @niden @andresgutierrez Could you please take a look

@sergeyklay
Copy link
Contributor

@mbrostami Could you please rebase, update CHANGELOG.md and cover issues by small test you successfully solved

@mbrostami
Copy link
Contributor Author

@sergeyklay Sure.

@mbrostami
Copy link
Contributor Author

@sergeyklay can you please check appveyor error? Is it my bad!?

@Jurigag
Copy link
Contributor

Jurigag commented Dec 13, 2016

Inner Exception: Unable to connect to the remote server no

@mbrostami
Copy link
Contributor Author

This issue (#12395) will also be fixed by this PR.

Copy link
Contributor

@sergeyklay sergeyklay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please take a look at comment for getMessagesFor and update CHANGELOG.md by adding last issue.

Thanks

this->_messages[name] = group;

return group;
return this->get(name)->getMessages();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in general looks good to me. But it bothers me that previously this method always returned the Group but now it can throw Exception. I think this behaviour should be the same now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, It should be backward compatible, I missed that.

@mbrostami
Copy link
Contributor Author

mbrostami commented Dec 14, 2016

@sergeyklay It's ready.

@sergeyklay
Copy link
Contributor

@mbrostami Could you please rebase onto 3.0.x

@mbrostami
Copy link
Contributor Author

@sergeyklay Done

@Jurigag
Copy link
Contributor

Jurigag commented Dec 20, 2016

Squash please.

@sergeyklay sergeyklay merged commit c505c68 into phalcon:3.0.x Dec 20, 2016
@sergeyklay
Copy link
Contributor

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants