Question

I cannot get $this->generateUrl() work, but it's work from my controller, or should I define 'setAction' using another way ?

class UserLoginType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('POST')
            ->setAction($this->generateUrl('fos_user_security_check'))
            ->add('username', 'text')
            ->add('password', 'password')
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'user_login';
    }
}
Was it helpful?

Solution

If you are building your form in a separate class and not in your controller, you should pass the action to the form type like this:

// In your controller

$form = $this->createForm(new FormType(), $object, array(
    'action' => $this->generateUrl('target_route'),
    'method' => 'POST',
));

The AbstractType does not contain any method generateUrl(), that's why you can't set the action in the type directly. You can find details here: http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form

OTHER TIPS

Yes, in the Form class, function buildForm , put this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->setMethod('POST')
        ->setAction( $options["data"]["action"])
        ->add('name', TextType::class, array('label' => 'Nombre'))
    ;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top