-
-
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
[Form] Added documentation for Form Events #3704
Conversation
This is an initial WIP. It is far from being finished, but the final goal is to completely describe Form Events in the component's documentation, and not only in the cookbook, which doesn't formally explains form events. |
Wow, thank you! Please note that there is already an article about form events in the cookbook: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html You might be able to get some usefull things from it. At least, you should remove all things you put in the components article from that cookbook article and add a reference (or multiple references) to this new article. In the end, we should have a components article telling you everything you need to know about Form Events and a cookbook article which gives you a short introduction and framework specific things. If you do not want to do this, someone of the community can do it after this one is merged. :) Again, big thanks for getting this roling! |
The Form component provides a structured process letting you customize your | ||
forms, by making use of the EventDispatcher. Using form events, you may | ||
modify information or fields at different points of time. From the form's | ||
pre-population to the submission of the request's data. |
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.
This should not be indented (that something specific for the component introduction articles, I don't know why we did that...)
Done. Also added a bit of code for adding a form event listener and a form event subscriber when using the form builder. |
Form Events | ||
=========== | ||
|
||
The Form component provides a structured process letting you customize your |
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.
to let you customize
👍 Great article! I really like it! |
|
||
The ``PRE_SET_DATA`` event is dispatched when | ||
:method:`Form::setData() <Symfony\\Component\\Form\\Form::setData>` | ||
is called. It can be used 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.
this is somewhat duplicating the previous paragraph. I think we have to tell when it happends, like: "The PRE_SET_DATA
event is dispatched in the beginning of the Form::setData()
method."
|
||
The ``SUBMIT`` event is dispatched just before the | ||
:method:`Form::submit() <Symfony\\Component\\Form\\Form::submit>` method | ||
transforms back the normalized data to the model and view data. |
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.
this is wrong. the normalized data is not transformed back to the view data, as it was just build from the submitted view data
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.
Actually, it is. See https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L632
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
$viewData = $this->normToView($normData);
Rebased commits once again. |
WOW, this is really incredible! Great great work! I have one thought. There are basically 2 sections to this entry: (A) explaining the form lifecycle and different events and (B) how to actually register event listeners/subscribers. I think we should change their order: explain how to register listeners first, then explain the event second. The reason is that I realized in the beginning (where we talk about the different events), we're talking about how to get at form data and the event object, as well as referring to events simply as SUBMIT, before really explaining that these are constants on FormEvents. If you reverse the order of these sections, I think it's an even better entry. What do you guys think? After this issue (and I left one other small comment), this is ready to merge! Great work to everyone! |
Hum, not sure. I just made the change locally, and it actually feels weird talking about form event listeners and subscribers before actually describing the events and the workflow. If we invert the sections, we need to rephrase a few things so it feels fluid. |
Ok, let's keep it how it is - but yea, I like Thanks for checking it out :) |
Done. And also rebased the commits. |
Well then, I guess it should be ready for merge, now! |
validate the CSRF token. | ||
|
||
B) The ``FormEvents::SUBMIT`` Event | ||
....................... |
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.
The underline doesn't match the title length.
Updated |
Fixed issue with travis build (underline too short) |
ping @weaverryan |
@csarrazi this PR is now ready. It'll be merged once @weaverryan has time to review and merge it. Since this PR add lots of stuff, it can take a little bit longer than smaller PRs. But don't worry, this great PR will never be missed by us :) |
@csarrazi But you can remove the [WIP] tag from the title for now. So, it's more obvious that the PR is finished. :) |
Thanks! I removed the [WIP] tag :) |
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
class AddEmailFieldListener implements EventSubscriberInterface |
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.
Maybe change the name to AddEmailFieldSubscriber
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.
Using listener for this is a Symfony convention, so let's stick with it (although I tend to agree with you that Subscriber is a better naming)
bump @weaverryan ! :) |
different steps of the workflow: from the population of the form to the | ||
submission of the data from the request. | ||
|
||
Registering an event listener very easy using the Form component. |
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.
is very easy
👍 |
Added initial documentation for Form Events: * List of events * Use cases for each event * Description of the events workflow * Flow charts for illustration Updated PR with comments from @wouterj and @mykiwi Took into account @cordoval's feedback Additional improvements * Added more examples from the Form component's code * Moved the cookbook's 'register a subscriber using a callable' section into the form events documentation Took into account @xabbuh's feedback Fixed missing new line Removed superfluous comma. Updated from feedback Moved the list of events down in the listener and subscriber section * Formatted the list as a table, exposing the constant values; * Added references to the form event information table; * Added caution sections for actions that should prevented while in each event. Added the form_events link in the components map file, a.k.a. 'Added the missing link' Added missing newlines Added cross-reference in the form events cookbook Updated based on Luis' feedback Added tables describing the form and event values: * Model data * Normalized data * View data * Event data Small fixes Updated a few wordings to make them more clear Changed important directive to caution Removed sidebar section about form extensions. Renamed ``{EVENT}`` to ``FormEvents::{EVENT}``, and added code snippet Last small changes Fixed underline too short
Sorry for getting the merge slowly - this is a truly wonderful effort and entry. I'm looking forward to showing this resource to people who are working with form events. Great work to you Charles and everyone :). Thanks! |
This PR was merged into the 2.3 branch. Discussion ---------- [Form] Added documentation for Form Events | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | >=2.3 | Fixed tickets | Dunno Added initial documentation for Form Events: * List of events * Use cases for each event * Description of the events workflow * Flow charts for illustration * Registering event listeners or subscribers in a form * Describing the data available for each event Commits ------- 98de95a [Form] Added documentation for Form Events
Added initial documentation for Form Events: