您好,我收到的数据是json格式的,在我的服务器站点上,我必须通过docine存储它们。一切正常,但当我收到日期时间格式数据时出现验证错误。我在控制器中的分离动作上测试这个案例:

public function indexAction($name)
{

    $em = $this->getDoctrine()->getManager();

    $test = new Test();
    $test->setName("Test");
    //$test->setStart(new \DateTime());
    $form = $this->createForm(new TestType(), $test);

    $store = array(
        "name" => "Test",
        "start" => new \DateTime()//will be something like *2014-04-09 11:11:11'
    );

    $form->submit($store);

    if ($form->isValid()) {
        $em->persist($test);
        $em->flush();
    } else var_dump($this->getErrorMessages($form));


    return $this->render('CodeTestBundle:Default:index.html.twig', array('name' => $name));
}

VAR转储是:
数组(size=1)“开始”=>
数组(大小=3)
0=>字符串“此值无效。”(长度=24)
  'date' =>
    array (size=3)
      'year' =>
        array (size=0)
          ...
      'month' =>
        array (size=0)
          ...
      'day' =>
        array (size=0)
          ...
  'time' =>
    array (size=2)
      'hour' =>
        array (size=0)
          ...
      'minute' =>
        array (size=0)
          ...

最佳答案

您的问题是表单框架希望每个日期和时间组件的视图数据由一个小部件处理,因为date time字段类型的默认小部件设置是choice。
如果将datetime字段配置为单个文本输入,则验证器将接收一个字符串而不是数组结构,并按预期处理它,而不执行任何其他转换。在您的情况下,字段配置如下:

$builder->add('start', 'datetime', array(
    'widget' => 'single_text',
    'input' => 'datetime'
));

10-07 12:38
查看更多