-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
[Components][Form] describe how to access form errors #4168
Conversation
|
||
Unless you enable the :ref:`error_bubbling <reference-form-option-error-bubbling>` | ||
option on a particular child form, ``getErrors()`` only returns the errors | ||
of the form it is accessed on. For debugging purposes, you can use the :method:`Symfony\\Component\\Form\\Form::getErrorsAsString` method which |
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.
unless you set $deep
to true
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.
Where do you want to set $deep
to true?
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.
sorry, I missed your next PR. This comment is no longer true.
(fyi, I responded to "Unless you enable the error_bubbling option on a particular child form, getErrors()
only returns the errors of the form it is accessed on.")
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.
No problem. Now that I understand your concerns. :)
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.
I think this note is a bit more abstract than I'd like. Here are the main points I'd like to say:
- Calling
getErrors()
on the main form object only returns global form errors - Calling
getErrors(true)
will return all of the form objects, but there's no way to know which field each is attached to - Calling
$form['firstName']->getErrors()
will return you errors for only thefirstName
field - Calling
getErrorsAsString()
is easy for debugging.
There are a lot of questions about form errors on stackoverflow etc. and don't think we have details anywhere currently. So I'd like to fully explain how to do what you want. The only unfortunate thing (I think there was an issue/PR about this) is that getErrors(true)
doesn't return any way (as far as a I can see) to know which error is attached to which field.
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.
Bah, nevermind - now I see that #4169 is here for the 2.5 features and that this is for 2.3.
So, obviously ignore (2), but I still think we should do 1, 3 and 4.
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.
@xabbuh I see that you put #4169 on hold for this, so I came back. I think we should basically expand your code block to show 1, 3 and 4 in my above note, with a comment for each that says what they do:
// an array of FormError objects, but only errors attached to this form level (e.g. "global errors)
$errors = $form->getErrors();
// an array of FormError objects, but only errors attached to the "firstName" field
$errors = $form['firstName']->getErrors();
// ... etc
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.
... because nobody reads words, so let's show all the code right next to each other :)
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.
friendly ping @xabbuh! :)
3a7a1cc
to
71a8072
Compare
I'm really sorry for the delay, but I finally managed to update the pull request. |
If you enable the :ref:`error_bubbling <reference-form-option-error-bubbling>` | ||
option on a form, ``getErrors()`` will also return errors attached to | ||
child forms, but there is no way to determine which field an error is | ||
attached to. |
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.
Sorry, now that I read this note more closely, I'm not sure I agree (or probably more likely, I'm not sure it's phrased correctly). It sounds like you can set error_bubbling
to true
on the top-level form and then all the errors of the child fields will bubble up to it. But I don't think that's the case - it's set on a field, and then that field's error bubble to the form.
If I'm right, how about something like this:
If you enable the
error_bubbling
option on a field, callinggetErrors()
on the parent
form will include errors from that field. However, there is no way to determine which
field an error was originally attached to.
Thanks! And no worries about the delay - I'm catching up my self. And nice work with the above changes.
71a8072
to
5deccd2
Compare
@weaverryan Of course, you're right. |
Thanks Christian - it looks great now! I'll merge this up so we can add the details special for 2.5. Cheers! |
…xabbuh) This PR was merged into the 2.3 branch. Discussion ---------- [Components][Form] describe how to access form errors | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | The ``getErrors()`` and ``getErrorsAsString()`` methods haven't been documented before. This pull request adds a short description for them. This forms as basis for the solution of #3660. Commits ------- 5deccd2 describe how to access form errors
…rors() (xabbuh) This PR was merged into the 2.5 branch. Discussion ---------- [Components][Form] document $deep and $flatten of getErrors() | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#9918) | Applies to | 2.5+ | Fixed tickets | #3660 This is based on #4168. Commits ------- 0245e91 [Form] document $deep and $flatten of getErrors() 4221db8 describe how to access form errors
The
getErrors()
andgetErrorsAsString()
methods haven't been documented before. This pull request adds a short description for them. This forms as basis for the solution of #3660.