Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST form validation in symfony 2 - how to test post

The form is never valid, but $form->getErrors() gives no error. Since its gonna be a REST API test it with DEV HTTP client

test data is

Header: Content-Type:application/json

Body: {"username":"sad","password":"123","email":"[email protected]"}

I don't have any validation.yml file

Is there any any method to find out whats going wrong (error Message)?

public function postUserAction(Request $request)
{

    return $this->processForm(new User(),$request);
}

private function processForm(User $user, Request $request )
{


    $form = $this->createForm(new UserType(), $user);
    $form->handleRequest($request);

    if ($form->isValid()) {

         return array('form' => 'valid');
    }

    return \FOS\RestBundle\View\View::create($form->getErrors(),400);
}
like image 918
user1930254 Avatar asked Mar 07 '14 02:03

user1930254


2 Answers

After a little debug: isValid() checks form several things eg if the form has been submitted. It wasnt, so i changed to

...
        $form = $this->createForm(new UserType(), $user);
        //$form->handleRequest($request);
        $form->submit($request);

        if ($form->isValid()) {
...

now its valid.

like image 82
user1930254 Avatar answered Oct 29 '22 16:10

user1930254


When debugging a form validation,

use $form->getErrorsAsString() instead of $form->getErrors().

(which will run in to deep level including the form children.)

like image 23
xdazz Avatar answered Oct 29 '22 15:10

xdazz