Is there a way to force a form bind in Symfony2?

59 views Asked by At

I have a form type

class LoginFacebookType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('access_token', 'text', array("constraints" => array(
                new Assert\NotBlank(),
                new Assert\Length(array("max" => 512))                        
            )))
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'facebook_login';
    }
}

Then, I use it on the controller as:

$facebookLoginForm = $this->createForm(new LoginFacebookType());
$facebookLoginForm->handleRequest($request);
if($facebookLoginForm->isValid())
{
    //do something
}
else
{
    //Debug form errors
    print_r($facebookLoginForm->getErrorsAsString()); die();
}

My questions is: If I made a request with a single param called "facebook_login[access_token]" i get all errors on the controller, like a very big access token, or not passing the csrf_token (This is ok). But if I made a request without any parameters, I get isValid = false, but error list is empty.

I want an "access_token field is required" and "invalid csrf_token" or something like that.

How can I achieve it?

1

There are 1 answers

1
Igor Pantović On BEST ANSWER

That is because form will not be submitted at all if there is no "facebook_login" key in POST data. You can force it to submit replacing $facebookLoginForm->handleRequest($request); with:

$facebookLoginForm->submit($request->request->get('facebook_login', []));