1

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?

Pipe
  • 2,379
  • 2
  • 19
  • 33

1 Answers1

2

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', [])); 
Igor Pantović
  • 9,107
  • 2
  • 30
  • 43
  • Thank you, it worked. Do you know if this method can be used with forms containing `file` fields? – Pipe Jan 13 '16 at 19:38