Here is a small recap sheet on the forms in symfony nested and a small special link to add embedForm on the fly.
- Definition and basic example of application
- Customizing the display
- Forms i18n translation
- forms added to the board fly
- Definition and basic example application: A
If you need users, you have to install the plugin sfDoctrineGuardPlugin. No matter what you use to manage your users, the data schema is the same. As long as we want our users to decorate an avatar, wish them their birthday, learn their gender (sex), it will have to add fields.
Thus we have 2 possibilities, either
- we modify the schema of the plugin, but it's a shame because the sudden he'll have to do it for each new site where you will need to do that.
- is, we change the general pattern by adding a table that can be called Profile for example. We define this table with the fields you want (avatar, gender, birth_date, etc.) and defines a "one to one" setting with table users. (We can even go further into the portability of the code by directly creating a plugin management staff profiles, and in two control lines, it won for new sites)
So here's to new schema.yml Table Profile
Well, first step ends, but for now, we just 2 models, although related, but each of which defines two different forms. We'll have to pick
form in the user profile form (always a concern for code portability and in order to create a plugin, at least that's my vision)
Symfony makes it easy to manage embedded forms. Just change the method configures the Profile form and add these lines:
I explain briefly my code which I think is quite explicit:
Here, I set my profile form. I instantiate a form of class sfGuardRegisterForm and I pass the user connected to the profile through the scheme.
So if I display $ form, I have 2 forms overlapping, being one single form for the user.
I've also shown how one can alter the way a normal form form board, such as labels.
2. Customizing the display
The above example does not agree with me totally, if you test your home, you probably know what I mean, the display is not correct.
First I totally mix the 2 forms. For now it gives me the 2 forms in the same table, however, the form sfGuardRegisterForm is displayed with a label: "User", which shifts the fields of a column. This is not what I want. Also, I can change the view by going to edit the corresponding file or _form.php and instead of displaying just $ form, it will display the items separately. It gives
AC:
It correctly shows us the desired form, perfect.
3. Forms translation i18n
Sometimes you have to do the translation of content, for example, if you make a blog for many languages, you create the Article model and you configure it as below so that it is translatable into the languages specified.
A small build - all created in his usual form classes and model. So if we will see in the lib, we can see the class ArticleTranslationForm.class.php
It does not affect the aim of the subject is just how to display it, it goes straight to the point, the method configures the form, we will insert our translation form as follows:
It will display well in the form of creating article, both forms of translation is that simple but when you do not know, we can not invent it;)
4. jaoutés embedded forms on the fly
http://www.nacho-martin.com/dynamic-embedded-forms-in-symfony very
0 comments:
Post a Comment