Created
March 23, 2011 15:32
-
-
Save webmozart/883293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected function _editAction(Post $post) | |
{ | |
$em = $this->get('doctrine.orm.default_entity_manager'); | |
$factory = $this->get('form.factory'); | |
$form = $factory->create(new PostFormType()); | |
$form->setData($post); | |
if ($this->get('request')->getMethod() === 'POST') { | |
$form->bindRequest($this->get('request')); | |
if ($form->isValid()) { | |
$em->persist($post); | |
$em->flush(); | |
return new RedirectResponse($this->generateUrl('hello_index')); | |
} | |
} | |
return $this->render('MyHelloBundle:Hello:edit.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\HelloBundle\Form; | |
use Symfony\Component\Form\FormBuilder; | |
use Symfony\Component\Form\Type\AbstractType; | |
use My\HelloBundle\Entity\Comment; | |
class PostFormType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder | |
->add('title') | |
->add('file') | |
->add('content') | |
->add('abstract') | |
->add('enabled') | |
->add('publicationDateStart', 'date') | |
->add('commentsDefaultStatus', 'choice', array( | |
'choices' => Comment::getStatusCodes(), | |
)) | |
// the second parameter, $type, is null because we use auto-creation | |
->add('tags', null, array( | |
'expanded' => true, | |
'multiple' => true, | |
)) | |
->build('author', 'form', array('data_class' => 'My\HelloBundle\Entity\Author')) | |
->add('firstName') | |
->add('lastName') | |
->end(); | |
} | |
public function getDefaultOptions(array $options) | |
{ | |
return array( | |
'data_class' => 'My\HelloBundle\Entity\Post', | |
); | |
} | |
public function getName() | |
{ | |
return 'postform'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% form_theme form _self %} | |
{{ form_enctype(form) }} | |
{{ form_widget(form) }} | |
{{ form_widget(form.firstName) }} | |
{% for child in form %} | |
{{ form_widget(child) }} | |
{% endfor %} | |
{{ form_widget(form.firstName, { 'attr': { 'class': 'foobar' } }) }} | |
{{ form_label(form.firstName, 'My label') }} |
But I really would like to have the thoughts of @bschussek on this :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I forgot to say that your domain objects must implement the
ToIdTransformable
interface: