Skip to content

Loading…

Binding a backbone.js PUT request to a form #326

Closed
pulse00 opened this Issue · 11 comments

5 participants

@pulse00

We're using backbone.js with the FOSRestBundle as it's backend. Backbone claims to use a standard REST API format when syncing it's model with the backend. It seems the way the rest-bundle binds the parameters from the request is incompatible with backbone.js.

Consider the following formtype:

<?php

class UserType ...

$builder->add('firstname');
$builder->add('lastname');

When backbone syncs the user model, the PUT parameters have the following format:

{
  'firstname' : 'my first name',
  'lastname' : 'my last name'
}

The form in the PUT action of the rest controller will only validate on the following parameter format though:

{
  'user' : { 
    'username' : 'my first name',
    'lastname' : 'my last name'
  } 
}

So i'm wondering which of the above 2 formats is the proper REST-ful format?

@stof
FriendsOfSymfony member

you can create forms with an empty root name, which is what you need here.

Btw, both format are Rest-full compliant. REST does not define how your representation should look like.

@willdurand
FriendsOfSymfony member

It's up to you as @stof said. The most important thing is to document your choice and to be consistent.

@pulse00

@stof @willdurand thanks for your input, makes sense!

@pulse00 pulse00 closed this
@dupuchba

Hey, I have the same issue but I can't make it work with @stof proposed...
So I built a "Rest" Api with a backbone.js front-end. Like @pulse00 , it sends format without the root name.

When I make a POST request with backbone, the request is not bind correctly... I have the same error s @pulse00 .
I tried to set the form with an empty root name with createNamed() but It's not working...

any idea guys?

@pulse00

@dupuchba our base FormType class for the backbone.js frontend looks like this:

<?php
namespace Acme\Demo\ApiBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

abstract class ApiType extends AbstractType
{
    /**
     *  {@inheritdoc}
     */
    public function getName()
    {
        return null;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection' => false,
        ));
    }
}

As a sidenote: After getting feedback from @bschussek, it's probably cleaner to simply use the Validation Component directly for input validation, and don't have the overhead of the FormComponent.

@dupuchba

@pulse00 thinks for the response, but that is exactly what I have and it returns errors as the values somehow are set to empty...
capture decran 2013-05-17 a 11 36 06
This is what my header send via POST for a Book object

@pulse00

it returns errors

what error do you get?

@dupuchba

Well, it's working now... I also forgot to set

nullable=true

to the entity. Thanks a lot for your help :)

@mvrhov

getName function in type returns form type name and not a from name. The fom name is specified when the form instance is created.
Also the from type names returned by getName are supposed to be unique.

@stof
FriendsOfSymfony member

And they cannot be null either.

@pulse00

@mvrhov @stof the above solution worked for us, although as i've mentioned that for simple input validation of a REST API you'd probably be better off by using the Form component directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.