New in symfony 1.2: God save the nested forms

In symfony 1.1, we introduced a new form sub-framework. It is a great step forward for symfony, even if the learning curve is a bit steep.
We worked a lot to make it even better for symfony 1.2, and more importantly simpler for newcomers.
One of the form framework strengths is its ability to deal with nested forms. A form can embed a form which in turn can also embed another form (and so on):
$article = ArticlePeer::doSelectOne(new Criteria()); $articleForm = new ArticleForm($article); $authorForm = new AuthorForm($article->getAuthor()); $companyForm = new AuthorForm($article->getAuthor()->getCompany()); $authorForm->embedForm('company', $companyForm); $articleForm->embedForm('author', $authorForm);
The articleForm
renders as show below:
Another great feature of the form framework is its ability to automatically
serialize forms. As the forms above are Propel ones, a simple call
to $articleForm->save()
will automatically update the $article
object with
the submitted and validated values and save it back to the database.
But there was a problem in symfony 1.1. The author
and the company
objects were not saved automatically. So, you had to override the save()
method to get the validated data and update the objects manually.
Nothing impossible to do, but really annoying as the form framework
already had all the needed information to make it automatic.
This has been implemented in symfony 1.2 and so will be available with
the upcoming beta 2. That's right, a single call to $form->save()
will now update the article,
the author, and the company objects. That's a great news by itself, but there is another one: The new admin generator
also takes this new feature into account.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Because there are a lot of possibilities with Doctrine and It's easy to get paralyzed.
Is it better practice to put the embedForm in the form class or do it in the action. I had found the function initially, but I have it in a new form class and I can't figure out if that's what is causing validation issues.
This post comes at a perfect time for me, I've tried to use this feature on a brand new project, but I have to deal with an issue that many users may have to face on their own project.
On my Article form, I've added an author embeded form, but only on the purpose to add an new author.
So I have 3 important fields on my form:
- A select field with the existing authors
- A checkbox "Add a new Author" that I've added
- An empty author form, to add a new Author
Here is the comportment that I want to have :
if "Add a new Author" is checked
I have to desactivate the validator on the existing authors select field
And then save the new Author, and link it to the article
else
I have to desactivate the "new Author" embeded form
And handle the basic existing authors field
Thanks to sfValidatorCallback, I know that I can ADD some custom validators depending on the values submited.
But how can I properly DISABLE some validators depending on the "Add a new author" checked test ?
I know that this comment might seem confused, so here is the form topic that I've created n this subject, with screenshots and source code.
sh1ny: Yes this works for Doctrine as well. I've just noticed in 1.1 you have to manually run doctrine:build-forms app. In 1.2 it will automatically build forms, filters, and models while running doctrine:build-all
Does this work for merged forms as well?
How we can approach this when using Admin Generator?
Will it be possible to dynamically add fields ? For example, when you have to edit an invoice, you can have a variable number of lines which may be added dynamically via JS.
For the moment, in my Symfony 1.1 project, I treat my lines as separate forms and I "link" them with my array of Propel line objects (they are linked with the same array index).
This solution works but it doesn't appear reliable to me. It would be appreciable to have a built in solution for that...
Thx
I have used embedded forms and the embedded objects were also saved by doctrine
I think :
$companyForm = new AuthorForm($article->getAuthor()->getCompany());
should be :
$companyForm = new CompanyForm($article->getAuthor()->getCompany());
Great feature this nested thing !
@Arne Yes this already worked with Doctrine. It was a side affect of how Doctrine handles relationships when using the $object->fromArray() method and propel doesn't.