dsdfd2322 2015-08-03 20:22
浏览 30

Symfony @Paramconverter和通过FormType更新实体失败

I am building an REST API with Symfony 2.7.2, using Doctrine, FOSRestBundle and JMSSerializerBundle. I was creating controller for my Address class using this pattern.

My Problem is that using default id->entity ParamConverter and passing converted Entity to a form type for updating it causes tha validation to fail as shown below. BUT if i replace it with an old-school $id and I fetch the entity myself from repository, it works fine.

Is it bug or have I missed something?

Relevant code follows.

So I have a controller method for get (HTTP GET) entity by id:

/**
 * @Route("addresses/{id}", requirements={"id" = "\d+"}, name="base_address_entity")
 * @Method({"GET"})
 */
public function detailAction(Address $address)
{
    return $address;
}

(works fine)

Another action for create new entity (HTTP POST)

/**
 * @Route("addresses")
 * @Method({"POST"})
 */
public function insertAction(Request $request)
{
    return $this->processForm(new Address(), $request);
}

(also works fine)

..and an update action (HTTP PUT), using default id ParamConverter as in the DetailAction:

/**
 * @Route("addresses/{id}", requirements={"id" = "\d+"}, name="base_address_create")
 * @Method({"PUT"})
 */
public function updateAction(Address $address, Request $request)
{
    return $this->processForm($address, $request);
}

and this one fails with an unspecified form error:

"code": 400,
"message": "Validation Failed",
"errors": {
    "errors": [
        "This value is not valid."
    ],
    "children": {
        ...no errors in any of the of the other fields

The process form function is the same for both insertAction and updateAction:

public function processForm(Address $address, Request $request)
{
    /** @var AddressRepository $addressRepository */

    $form = $this->createForm($this->get('address.form_type'), $address);

    $form->submit($request->get($form->getName()));

    if ($form->isValid())
    {
        $addressRepository = $this->get('address.repository');
        $addressRepository->save($address);
        return $this->get('success');
    }
    return $form;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在matlab中Application Compiler后的软件无法打开
    • ¥15 想问一下STM32创建工程模板时遇到得问题
    • ¥15 Fiddler抓包443
    • ¥20 Qt Quick Android 项目报错及显示问题
    • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
    • ¥15 oracleBIEE analytics
    • ¥15 H.264选择性加密例程
    • ¥50 windows的SFTP服务器如何能批量同步用户信息?
    • ¥15 centos7.9升级python3.0的问题
    • ¥15 安装CentOS6时卡住