Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change a Form Class action on build?

On a form classe's buildForm method (AbstractType derived) can I set the action of that form? What I want to do is similar to the setAction method that I can use when building an embedded form:

$form = $this->createFormBuilder()
  ->setAction($this->generateUrl('my_action'))
  ->add('field', 'text')
  ->add('button', 'submit');

I mean, is the setAction an equivalent to form classes?

like image 927
Nelson Teixeira Avatar asked Sep 05 '13 23:09

Nelson Teixeira


1 Answers

You get access to the same form builder in the buildForm method, so just calling the setAction on it will work:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->setAction($path);
}

The question is how you pass the $path to your form type. One of the ways would be to pass it as an option when creating the form. But if you're passing the $path anyway, why not just set the action itself?

$form = $this->createForm(new MyType(), $object, array(
    'action' => $this->generateUrl('my_action'),
));

Another way would be to inject the router to the form type and use it to generate the URL, but I don't think it's a good idea to make that kind of decisions in a form type.

like image 74
Elnur Abdurrakhimov Avatar answered Nov 10 '22 15:11

Elnur Abdurrakhimov