Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Typo in Exception in Symfony\Component\Form\Form #22103

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

Closed
desmax opened this issue Mar 22, 2017 · 8 comments
Closed

Typo in Exception in Symfony\Component\Form\Form #22103

desmax opened this issue Mar 22, 2017 · 8 comments
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Form

Comments

@desmax
Copy link
Contributor

desmax commented Mar 22, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version master

File https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php

throw new RuntimeException('A cycle was detected. Listeners to the PRE_SET_DATA event must not call setData(). You should call setData() on the FormEvent object instead.');

I believe here is a typo, in first case there should be getData instead of setData.

@fancyweb
Copy link
Contributor

fancyweb commented Mar 22, 2017

This is not a mistake, the message is right and the exception is justified, cf the doc of form events (http://symfony.com/doc/current/form/events.html) and especially this warning :

During FormEvents::PRE_SET_DATA, Form::setData() is locked and will throw an exception if used. If you wish to modify data, you should use FormEvent::setData() instead.

@desmax
Copy link
Contributor Author

desmax commented Mar 22, 2017

Ok, makes sense. but maybe it should be clarified which setData is meant in the first place.

P.S. I got this when I called $event->getForm()->getData();

@fancyweb
Copy link
Contributor

fancyweb commented Mar 22, 2017

Ok I see exactly the problem and you are right, the message is not logic for the DX.

That happens when no data was set when the form is built, and then you use $event->getForm()->getData(); in a FormEvents::PRE_SET_DATA event. I think the right way is to use $event->getData() in this event but there is no message about this.

When you try to get the data directly from the form, as it was not set, it tries to set the default data with a Form::setData() call that will dispatch the FormEvents::PRE_SET_DATA event once again and end up in a cycle...

We can throw another exception with a better message.

@fancyweb
Copy link
Contributor

That also mean that if no data was already set for the form, in a FormEvents::PRE_SET_DATA event, we cannot have access to the Form::getNormData() and Form::getViewData() methods as they also call the FormEvent::setData() and will throw the same misleading exception.

@stof
Copy link
Member

stof commented Mar 22, 2017

well, getting the data from the form before it is set in it does not make any sense anyway. It is not there yet.

@fancyweb
Copy link
Contributor

I agree but the problem is that if you try to do it, you get a misleading / unrelated message.

@javiereguiluz
Copy link
Member

We are always in favor of improving error messages, so would @desmax or @fancyweb be interested in contributing a pull request to improve this error message a little? Thanks!

@fancyweb
Copy link
Contributor

@javiereguiluz I will work on improving this.

@javiereguiluz javiereguiluz added DX DX = Developer eXperience (anything that improves the experience of using Symfony) Form labels Mar 24, 2017
fabpot added a commit that referenced this issue Mar 31, 2017
…in a PRE_SET_DATA listener and the data has not already been set (fancyweb)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Improve the exceptions when trying to get the data in a PRE_SET_DATA listener and the data has not already been set

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22103
| License       | MIT
| Doc PR        | -

Commits
-------

ef39b70 [Form] Improve the exceptions when trying to get the data in a PRE_SET_DATA listener and the data has not already been set
@fabpot fabpot closed this as completed Mar 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Form
Projects
None yet
Development

No branches or pull requests

5 participants