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

[Components][Form] describe how to access form errors #4168

Merged
merged 1 commit into from
Oct 28, 2014

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Aug 23, 2014

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.


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
Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

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.")

Copy link
Member Author

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. :)

Copy link
Member

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:

  1. Calling getErrors() on the main form object only returns global form errors
  2. Calling getErrors(true) will return all of the form objects, but there's no way to know which field each is attached to
  3. Calling $form['firstName']->getErrors() will return you errors for only the firstName field
  4. 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.

Copy link
Member

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.

Copy link
Member

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

Copy link
Member

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 :)

Copy link
Member

Choose a reason for hiding this comment

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

friendly ping @xabbuh! :)

@xabbuh xabbuh force-pushed the issue-3660-2.3 branch 2 times, most recently from 3a7a1cc to 71a8072 Compare October 20, 2014 21:13
@xabbuh
Copy link
Member Author

xabbuh commented Oct 20, 2014

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.
Copy link
Member

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, calling getErrors() 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.

@xabbuh
Copy link
Member Author

xabbuh commented Oct 27, 2014

@weaverryan Of course, you're right.

@weaverryan
Copy link
Member

Thanks Christian - it looks great now! I'll merge this up so we can add the details special for 2.5. Cheers!

@weaverryan weaverryan merged commit 5deccd2 into symfony:2.3 Oct 28, 2014
weaverryan added a commit that referenced this pull request Oct 28, 2014
…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
@xabbuh xabbuh deleted the issue-3660-2.3 branch October 28, 2014 08:45
weaverryan added a commit that referenced this pull request Nov 20, 2014
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants