Php Symfony使用表单更新实体

Php Symfony使用表单更新实体,php,symfony,Php,Symfony,我正在用基本的crud操作构建一个简单的restfull API 我试图通过PUT请求更新实体,但我的表单没有验证,并且在发送PUT请求时也没有给出任何错误。我做错了什么?仅供参考,具有相同表单类型的postAction确实有效 EventController::putEventsAction /** * Update excisting event. * * @return event */ public function putEventsAction($id, Request $r

我正在用基本的crud操作构建一个简单的restfull API

我试图通过PUT请求更新实体,但我的表单没有验证,并且在发送PUT请求时也没有给出任何错误。我做错了什么?仅供参考,具有相同表单类型的postAction确实有效

EventController::putEventsAction

/**
 * Update excisting event.
 *
 * @return event
 */
public function putEventsAction($id, Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $event = $em->find('JdsApiBundle:Event', $id);

    if(!$event) {
        $view = $this->view(["id" => $id], Codes::HTTP_NOT_FOUND);
        return $this->handleView($view);
    }

    $form = $this->createForm(EventType::class, $event);
    $form->handleRequest($request);

    if ($form->isValid()) {

        $em->persist($event);
        $em->flush();

        $view = $this->view($event, Codes::HTTP_OK);

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

    $logger = $this->get('logger');
    $logger->info((string) $form->getErrors(true));

    return (string) $form->getErrors(true);

    // return array(
    //  'form' => $form,
    // );
}
事件类型

class EventType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('startDate', DateTimeType::class, array(
                'widget' => 'single_text',
                'format' => 'yyyy-MM-dd', // this is actually the default format for single_text
            ))
            ->add('endDate', DateTimeType::class)
            ->add('description')
            ->add('color')
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection' => false,
            'data_class' => 'Jds\ApiBundle\Entity\Event'
        ));
    }
}

尝试在表单中添加
PUT
方法,如下所示:

$form = $this->createForm(EventType::class, $event, array('method' => 'PUT'));

我没有表格。这是一个来自JS前端(或开发中的邮递员)的ajax调用。Thx,它可以工作,但在发布前缺少一个报价,我无法编辑您的帖子。Thx用于您的回复。:)