Form绑定不绑定请求在Symfony 2.1 + FOSRestBundle中形成 [英] Form bind not binding request to form in Symfony 2.1+FOSRestBundle

查看:135
本文介绍了Form绑定不绑定请求在Symfony 2.1 + FOSRestBundle中形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSRestbundle实施其他API。现在说POST请求我得到正确的请求参数,如:

  Symfony \Component\HttpFoundation\ParameterBag对象

[parameters:protected] => Array

[rank] => 12
[city] => 1345
[comment ] =>'safd'



我的帖子操作代码是:

  / ** 
* @ Rest \View
* /
public function newAction(){

$ rank = new Rank();
$ form = $ this-> createForm(new RankType(),$ rank);
$ form-> bind($ this-> getRequest());
if($ form-> isValid()){
//。 $用户>冲洗();

$ em = $ this-> getDoctrine() - > getManager();
$ em-> persist($ rank);
$ em-> flush();
$ response = new Response();
$ response-> setStatusCode($ statusCode);
$ view = View :: create()
- > setData($ rank)
- > setFormat('json');

return $ this-> handleView($ view);
}

return $ this-> handleView(View :: create($ form,400));
}

但是 form-> isValid b

 在绑定表单窗体后,> getData()将显示:


MyProject \DataBundle\Entity\Rank Object

[city:MyProject\DataBundle\Entity\Ranking:private] =>
[rank:MyProject\DataBundle\Entity\Ranking:private] =>
[comment:MyProject\DataBundle\Entity\Ranking:private] =>

RankType代码:

  class RankType extends AbstractType 
{
/ **
* {@inheritdoc}
* /
public function buildForm(FormBuilderInterface $ builder, array $ options)
{
$ builder-> add('rank');
$ builder-> add('city');
$ builder-> add('comment');

$ b $ **
* {@inheritdoc}
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'Myporject \DataBundle\Entity\rank',
'csrf_protection'=> false,
));

$ b / **
* {@inheritdoc}
* /
public function getName()
{
return '秩';


验证会抛出像城市一样的错误,并且排名不应该是null



任何建议为什么 $ form->绑定没有绑定值?

解决方案

我认为,我已经解决了我的应用程序中的相同问题:)

在你的表单中:getName()方法返回'rank' - 这是表单的名称AND这是这个表单数据的命名空间:) Symfony2将使用它来绑定。


$ b $因此,当你发送这样的数据时:

array('rank'=>'somevalue','city' =>'comecityname','comment'=>'somecomment')



它不起作用,因为bind方法会搜索位于'rank'命名空间下的数据数组将找到字符串'somevalue'。

将数据格式更改为:

  array('rank'=> array('rank'=>'somevalue','city'=>'comecityname','comment'=>'somecomment'))

并用REST发送它 - 然后它应该工作



我希望这将有助于

I'm implementing rest API using FOSRestbundle. Now say for POST request i'm getting request parameters properly like:

Symfony\Component\HttpFoundation\ParameterBag Object
(
    [parameters:protected] => Array
        (
            [rank] => 12
            [city] => 1345
            [comment]=> 'safd'
        )

)

My post action code is :

/**
 * @Rest\View
 */   
public function newAction(){

    $rank= new Rank();       
    $form = $this->createForm(new RankType(), $rank);
    $form->bind($this->getRequest());
    if ($form->isValid()) {
      //.  $user->flush();

      $em = $this->getDoctrine()->getManager();
      $em->persist($rank);
      $em->flush();
      $response = new Response();
      $response->setStatusCode($statusCode);
      $view = View::create()  
        ->setData($rank)
        ->setFormat('json');

      return $this->handleView($view);
    }

    return $this->handleView(View::create($form, 400));
}

But form->isValid fails due to setting null values to form.

After Binding form form->getData() will display :


MyProject\DataBundle\Entity\Rank Object
(
    [city:MyProject\DataBundle\Entity\Ranking:private] => 
    [rank:MyProject\DataBundle\Entity\Ranking:private] => 
    [comment:MyProject\DataBundle\Entity\Ranking:private] => 
)   

RankType code :

class RankType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('rank');
        $builder->add('city');
        $builder->add('comment');
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'        => 'Myporject\DataBundle\Entity\rank',
            'csrf_protection'   => false,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'rank';
    }
}

Validation will throw error like city and rank should not be null

Any suggestions why $form->bind not binding values?

解决方案

I think, that I've just resolved the same problem in my app :)

Look at your form: getName() method returns 'rank' - and this is the name of the form AND this is the namespace for this form data :) Symfony2 will use it to bind.

So, when you will send data like that:

array('rank'=> 'somevalue', 'city' => 'comecityname', 'comment' => 'somecomment')

it will not work, because bind method will search for array of data placed under 'rank' namespace and will find string 'somevalue'. Form data will remain empty.

Change format of data to that:

array('rank' => array('rank'=> 'somevalue', 'city' => 'comecityname', 'comment' => 'somecomment'))

and send it with REST - then it should work

I hope this will help

这篇关于Form绑定不绑定请求在Symfony 2.1 + FOSRestBundle中形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆