Symfony:带有rfc 3339字符串输入的日期时间表单类型

时间:2016-01-05 07:23:02

标签: php forms symfony datetime

我试图使用日期时间类型

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

我的表单日期格式是RFC 3339

    [start_date] => '2016-01-04T16:01:25+00:00'

错误

    The parsed date was invalid, The separation symbol could not be found, Unexpected data found., Unexpected data found., Unexpected data found., Unexpected data found., Unexpected data found., Data missing

似乎datetime类型只期望日期为格式为2016-01-04 16:01:25的字符串,并且此格式无法从表单类型配置中更改

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php#L171

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php#L68

想知道这是否是理想的行为或问题

注意

formatdate_format选项都适用于视图数据而不是输入数据。

堆栈跟踪

at DateTimeToStringTransformer ->reverseTransform ('2016-01-04T16:01:25+0000') 
in vendor/symfony/symfony/src/Symfony/Component/Form/ReversedTransformer.php at line 46   + 
at ReversedTransformer ->transform ('2016-01-04T16:01:25+0000') 
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 1087   + 
at Form ->modelToNorm ('2016-01-04T16:01:25+0000') 
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 352   + 
at Form ->setData ('2016-01-04T16:01:25+0000') 
in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 57   + 
at PropertyPathMapper ->mapDataToForms (array('start_date' => '2016-01-04T16:01:25+0000')), object(RecursiveIteratorIterator)) 
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 386   + 
at Form ->setData (array('start_date' => '2016-01-04T16:01:25+0000')) 
in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 478   + 
at Form ->initialize () 
in vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 226   + 
at FormBuilder ->getForm () 
in vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 40   + 
at FormFactory ->create ('app_test_form', array('start_date' => '2016-01-04T16:01:25+0000')), 'items' => array()), array()) 
in vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 282   + 
at Controller ->createForm ('app_test_form', array('start_date' => '2016-01-04T16:01:25+0000')), 'items' => array())) 
in src/AppBundle/Controller/DummyController.php at line 39   + 

2 个答案:

答案 0 :(得分:0)

您似乎还需要添加格式

参考详情here

  

想知道这是否是理想的行为或问题

这似乎是根据文档的行为,请参考line 140

// Change the input to a HTML5 date input if
    //  * the widget is set to "single_text"

    //  * the format matches the one expected by HTML5

    //  * the html5 is set to true


add('start_date', 'date', array(
        'widget' => 'single_text',
        'format' => 'c'))

参考question

答案 1 :(得分:0)

$builder->add('start_date', 'datetime', array(
        'widget' => 'single_text',
        'input'  => 'string',
        'date_format' => 'yyyy-MM-dd  HH:mm:ss'
    ));
试试这个,希望对你有所帮助