Monday, 15 April 2013

php - Symfony2 Separate crud form from controller -



php - Symfony2 Separate crud form from controller -

i used ./app/console generate:doctrine:crud command , noticed how created new methods in controller: "createcreateform", "createeditform" , "createdeleteform". there way create these crud forms within single formtype instead?

when @ generated createeditform method, looks this:

private function createeditform(myentity $entity) { $form = $this->createform(new formtype(), $entity, array( 'action' => $this->generateurl('myentity_update', array('id' => $entity->getid())), 'method' => 'put', )); $form->add('submit', 'submit', array('label' => 'update')); homecoming $form; }

the buildform method in formtype.php

public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('name') ->add('description') ->add('save', 'submit') ; }

is there improve practise, passing settings straight buildform method?

you can using formtypeinterface#setdefaultoptions:

use symfony\component\optionsresolver\optionsresolverinterface; public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'method' => 'put', )); }

you shouldn't action option, that's should defined controller. form type shouldn't know url architecture of application.

php symfony2

No comments:

Post a Comment