Skip to content

Commit

Permalink
[cookbook] Added new example for the Symfony2 Forms chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Sep 25, 2011
1 parent 59c653c commit 9f7e4cf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 38 additions & 5 deletions cookbook/symfony2/mastering-symfony2-forms-with-propel.markdown
Expand Up @@ -125,7 +125,7 @@ then persisting it after a form submission can be done when the form is valid:
// ...
public function newAction()
{
{
$book = new Book();
$form = $this->createForm(new BookType(), $book);

Expand All @@ -138,13 +138,13 @@ then persisting it after a form submission can be done when the form is valid:
$book->save();

return $this->redirect($this->generateUrl('book_success'));
}
}
}
}

return $this->render('AcmeLibraryBundle:Book:new.html.twig', array(
'form' => $form->createView(),
));
}
));
}
}
{% endhighlight %}

Expand Down Expand Up @@ -310,6 +310,39 @@ doesn't handle the add/remove abilities in the view. You have to write some Java

![](./images/many_to_many_form.png)

But in this example, you'll always create new objects. If you want to select existing authors when you create new books, you'll use the `model` type:

{% highlight php %}
<?php
// src/Acme/LibraryBundle/Form/Type/BookType.php
namespace Acme\LibraryBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class BookType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('title');
$builder->add('isbn');

//$builder->add('author', new AuthorType());
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
));
}

// ...
}
{% endhighlight %}

The `ModelType` is part of the `PropelBundle` bundle. You'll get the following result:

![](./images/many_to_many_form_with_existing_objects.png)


## Summary ##

The Symfony2 Form Component doesn't have anymore secrets for you and to use it with Propel is really
Expand Down

0 comments on commit 9f7e4cf

Please sign in to comment.