-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add getErrorsAsArray method #7512
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
Conversation
Usefull to return form errors through JsonResponse modified: src/Symfony/Component/Form/Form.php modified: src/Symfony/Component/Form/Tests/CompoundFormTest.php modified: src/Symfony/Component/Form/Tests/SimpleFormTest.php
modified: src/Symfony/Component/Form/Tests/CompoundFormTest.php
For information, Scrutinizer errors are not due to my commits. I can't see what is wrong with Travis check on PHP 5.4. Is it possible to have the detail of the failure? When I click here: https://travis-ci.org/symfony/symfony/jobs/5879397, I simply have an endless "loading" message. |
the travis interface seems broken currently (I see a JS error in the console). You should report it in their issue tracker. And waiting for a fix, you could try to get the logs through their CLI or their API. |
@stof Thanks. In fact I get an error with Firefox 19.0.2 but not in Chrome. I opened a ticket on travis-web tracker: https://github.com/travis-ci/travis-web/issues/171 The failure concern this test, but I didn't touch it.
|
$template = $error->getMessageTemplate(); | ||
if (null !== $template) { | ||
$parameters = $error->getMessageParameters(); | ||
$errors[$key] = strtr($template, $parameters); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How it pissible translate this messages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not possible. :(
may be, we can introduce a little optional coupling by allow the end user to give a translatorInterface to getErrorsAsArray
so from the controller $form->getErrorsAsArray($translator);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raziel057 Does one $key
can contain more than 1 error? If yes - you should append they but not override
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Koc As you can see it on the tests I added, a field can contain multiple errors, but in this case there is only one error per $key. Do you have use cases with multiples error per $key?
@lyrixx Yes, I also thought about this solution. I think it's not so bad. I can add this option if @fabpot is ok for that.
@bschussek What do you think about this addition? |
The current implementation here is a bit flawed. It combines both local errors (indexed by integers) and nested errors (indexed by field names, which can be integers too) in one single data structure, which cannot work. I think this functionality should be merged into I can't really give any advice yet on further details. This needs to be thought through and it is definitely not trivial. |
If I can give my opinion on the expected format, this should look like |
@bschussek That seems like a nice idea to me. However, it seems to me About the array compatibility, would implementing |
@TomiS FormError instances are attached to a Form instance. So getting the field name is possible as you need to Form to get the errors. |
Ok, I spent yesterday playing with this thing and first tried to implement something similar to what @bschussek had in mind. Here's a diff of what I ended up with. There are a couple of problems with this approach, though. 1) The code becomes quite complex if we really want array compatibility and if it should be fully array serializable (to get json too). 2) I ended up repeating the tree structure already introduced by So... I also ended up trying something much more simple. Basically what I did was repeating the same implementation introduced in this PR but just in the ps. Good point @stof. And it's also kind of related to what I say here about duplicating tree structure. |
replaced by #9099 |
Usefull to return form errors through JsonResponse.
See #7205