Search code examples
symfonyfosuserbundlefosrestbundle

FOSRestBundle How to validate the registration?


I'm developing a RESTFul API in Symfony 2.3.* with FOSUserBundle and FOSRestBundle, and I'm having trouble understanding how to create a registration method.

My controller look like this :

class UserRestController extends FOSRestController
{
    //Other Methods ...

    public function postUserAction()
    {
        $userManager = $this->get('fos_user.user_manager');
        $user = $userManager->createUser();

        $param = $paramFetcher->all();
        $form = $this->createForm(new UserType(), $user);
        $form->bind($param);

        if ($form->isValid() == false)
            return $this->view($form, 400);

        $userManager->updateUser($user);
        return $this->view('User Created', 201);
    }

    //...
}

And my UserType class :

class UserType extends BaseType
{
    public function __construct($class = "User")
    {
        parent::__construct($class);
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('username', 'username')
                ->add('email', 'email')
                ->add('plainPassword', 'repeated', array(
                   'first_name' => 'password',
                   'second_name' => 'confirm',
                   'type' => 'password'
                ))
                ->add('lastname')
                ->add('firstname')
                ->add('job_position')
                ->add('phone')
                ->add('company_name')
                ->add('website')
                ->add('sector')
                ->add('address')
                ->add('city')
                ->add('zip_code')
                ->add('country')
                ->add('billing_infos_same_as_company')
                ->add('billing_address')
                ->add('billing_city')
                ->add('billing_zip')
                ->add('billing_country')
                ->add('putf')
                ->add('das');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Wipsea\UserBundle\Entity\User',
            'csrf_protection' => false
        ));
    }

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

When I test it, no matter what the form isn't valid and shows no error. And when I try to get the request :

"Validation Failed" "This form should not contain extra fields."

Is there a way to properly validate the form ?

EDIT : Updating my problem.


Solution

  • I would recommend you this tutorial in 3 parts - there is everything you need:

    If you want to provide complex user validation you should create UserType form and pass data to this form instead of directly setting all properties:

    public function postAction()
    {
        $user = new User();
    
        $form = $this->createForm(new UserType(), $user);
        $form->handleRequest($this->getRequest());
    
        if ($form->isValid()) {
            // propel version
            $user->save();
    
            $response = new Response();
            $response->setStatusCode(201);
    
            // set the `Location` header only when creating new resources
            $response->headers->set('Location',
                $this->generateUrl(
                    'acme_demo_user_get', array('id' => $user->getId()),
                    true // absolute
                )
            );
    
            return $response;
        }
    
        // return form validation errors
        return View::create($form, 400);
    }
    

    In part 2 of this tutorial you have all information about creating form, passing data and validating it with RestBundle.

    There is also a lot information about best practices using REST with Symfony2.