php - Symfony2 FOSRestBundle PUT Action FORM 返回空结果

标签 php symfony symfony-forms fosrestbundle

我正在使用 Symfony 2.2 和最新版本的 FOSRestBundle。因此,我设法使大部分操作都有效,但我传递 PUT 调用请求的 FormBuilder 似乎有问题。

我已经检查了请求对象,它来 self 的 Backbone.je 模型,因为它应该 (.save()) 但是在绑定(bind)到表单之后,实体返回只有导致 flush() 抛出错误的 id因为必填字段未填写。

Controller 中的 Action :

header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS ');
header('Allow GET, POST, PUT, DELETE, OPTIONS ');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, *');

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\Rest\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Greenthumbed\ApiBundle\Entity\Container;
use Greenthumbed\ApiBundle\Form\ContainerType;

class ContainerController extends FOSRestController implements ClassResourceInterface
{
/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new ContainerType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array(
        'form' => $form,
    );
}

/**
 * Get entity instance
 * @var integer $id Id of the entity
 * @return Container
 */
protected function getEntity($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GreenthumbedApiBundle:Container')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Container entity');
    }

    return $entity;
}

调用的表单:

namespace Greenthumbed\ApiBundle\Form;

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

class ContainerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('description')
            ->add('isVisible')
            ->add('type')
            ->add('size')
            ->add('creationDate')
            ->add('userId')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Greenthumbed\ApiBundle\Entity\Container',
            'csrf_protection' => false,
        ));
    }

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

到目前为止,我已经尝试了所有方法,但我对 Symfony 还很陌生,我不明白为什么 $entity 不包含请求收到的值。

仅供引用:我已经尝试手动执行此操作,例如使用请求的 ID 实例化容器类,并使用 setter 将值输入其中并且它工作得很好,我只是想像 Symfony 一样以正确的方式做事建议应该这样做。

非常感谢您。

最佳答案

尝试更改 putAction 中的以下行:

$form = $this->createForm(new ContainerType(), $entity);

到:

$form = $this->createForm(new ContainerType(), $entity, array('method' => 'PUT'));

关于php - Symfony2 FOSRestBundle PUT Action FORM 返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16976515/

相关文章:

php - codeigniter flashdata 在 false if 语句中工作

php - 如何在 php 中解析长值?

php - 基于 javascript 的幻灯片无法正常工作

php - 索纳塔管理过滤器 : Invalid parameter number: number of bound variables does not match number of tokens

php - 在 Formbuilder::add() 中配置 EntityType 时访问类的属性和方法

php - 修改 Alpine 闪现通知

php - 如何添加自定义 "under maintenance"页面?

templates - 如何在 Symfony2 环境中注册另一个(自定义)Twig 加载器?

symfony - Symfony2中ObjectManager和EntityManager的区别?

php - Symfony2 表单给出关于 FormView 的可捕获错误