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

[Form] Added documentation for Form Events #3704

Merged
merged 1 commit into from
May 6, 2014
Merged

[Form] Added documentation for Form Events #3704

merged 1 commit into from
May 6, 2014

Conversation

csarrazi
Copy link
Contributor

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

@csarrazi
Copy link
Contributor Author

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.

@wouterj
Copy link
Member

wouterj commented Mar 21, 2014

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

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

@csarrazi
Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

to let you customize

@xabbuh
Copy link
Member

xabbuh commented Mar 22, 2014

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

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

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

Copy link
Contributor Author

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

@csarrazi
Copy link
Contributor Author

Rebased commits once again.

@weaverryan
Copy link
Member

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!

@csarrazi
Copy link
Contributor Author

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.
In any case, I can also rephrase the sections of the documentation to refer to FormEvents::{EVENT_NAME} instead of simply {EVENT_NAME}.

@weaverryan
Copy link
Member

Ok, let's keep it how it is - but yea, I like FormEvents::{EVENT_NAME} better, and we can also add a quick note to the top with a link that points to the second section on usage. A very very quick example usage at the top of the chapter (not really doing anything, just showing the registration of an anonymous function on some form event, and the arguments of that callback) might be plenty to give people enough idea of how this works before explaining the events.

Thanks for checking it out :)

@csarrazi
Copy link
Contributor Author

Done. And also rebased the commits.

@csarrazi
Copy link
Contributor Author

csarrazi commented Apr 1, 2014

Well then, I guess it should be ready for merge, now!
What do you think about it now, @weaverryan? :)

validate the CSRF token.

B) The ``FormEvents::SUBMIT`` Event
.......................
Copy link
Member

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.

@csarrazi
Copy link
Contributor Author

csarrazi commented Apr 5, 2014

Updated

@csarrazi
Copy link
Contributor Author

csarrazi commented Apr 5, 2014

Fixed issue with travis build (underline too short)

@csarrazi
Copy link
Contributor Author

csarrazi commented Apr 8, 2014

ping @weaverryan

@wouterj
Copy link
Member

wouterj commented Apr 8, 2014

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

@xabbuh
Copy link
Member

xabbuh commented Apr 8, 2014

@csarrazi But you can remove the [WIP] tag from the title for now. So, it's more obvious that the PR is finished. :)

@csarrazi csarrazi changed the title [WIP] [Form] Added documentation for Form Events [Form] Added documentation for Form Events Apr 8, 2014
@csarrazi
Copy link
Contributor Author

csarrazi commented Apr 8, 2014

Thanks! I removed the [WIP] tag :)

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class AddEmailFieldListener implements EventSubscriberInterface
Copy link
Contributor

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

Copy link
Member

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)

@csarrazi
Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

is very easy

@aitboudad
Copy link
Contributor

👍

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

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!

@weaverryan weaverryan merged commit 98de95a into symfony:2.3 May 6, 2014
weaverryan added a commit that referenced this pull request May 6, 2014
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
@csarrazi csarrazi deleted the components/form/form_events branch May 6, 2014 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.