FormType默认输入值的形式相同 [英] FormType default input value in same form
问题描述
我有一个名为Task的实体,并为该表单构建一个Symfony TaskType.php。
我的目标是将endDate datetime字段默认设置为startDate datime字段的输入(这是必需的)。
我试过了,但它不起作用。
$ builder-> add('name');
$ builder-> add('startDate','datetime');
$ builder-> add('endDate','datetime',array(
'empty_value'=> array('year'=>'Year','month'=>' $'$''',
'required'=> false,
'data'=> isset($ options ['data'])?$ options [ 'data'] - > getEndDate():$ options ['data'] - > getStartDate(),
));
例外:
使用参数{1:test,2执行'INSERT INTO Task(name,startDate,endDate)VALUES(?,?,?)'时发生异常:2013-03-30 00 :00:00,3:null}:
SQLSTATE [23000]:完整性约束违规:1048'endDate'列不能为空
500内部服务器错误 - DBALException
1链接异常:
PDOException»
是的,当然是行不通的。我建议阅读关于表单是如何工作的,如果你没有时间阅读这个cheatsheet - http://blog.stfalcon.com/wp-content/uploads/2012/01/how_symfony2_forms_works.pdf 。
您可以在设置数据后设置该值。例如,试试:
$ form-> bindRequest($ request);
//现在你可以获取数据并设置
$ form-> get('endDate') - > setData($ form-> get('startDate') - > getData() );
希望它有帮助。
I have an Entity named Task and build a Symfony TaskType.php for the form. It is my aim to set the endDate datetime field by default to the input of the startDate datime field (which is required).
I tried this, but it doesn't work.
$builder->add('name');
$builder->add('startDate', 'datetime');
$builder->add('endDate', 'datetime', array(
'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
'required' => false,
'data' => isset($options['data']) ? $options['data']->getEndDate() : $options['data']->getStartDate(),
));
Exception:
An exception occurred while executing 'INSERT INTO Task (name, startDate, endDate) VALUES (?, ?, ?)' with params {"1":"test","2":"2013-03-30 00:00:00","3":null}:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'endDate' cannot be null 500 Internal Server Error - DBALException 1 linked Exception:
PDOException »
Yes, of course it doesn't work. I recommend to read about how form works, if you have no time you can just read this "cheatsheet" - http://blog.stfalcon.com/wp-content/uploads/2012/01/how_symfony2_forms_works.pdf.
You can set the value after data was set. For example, try:
$form->bindRequest($request);
// now you can get data and set
$form->get('endDate')->setData($form->get('startDate')->getData());
Hope, it helps.
这篇关于FormType默认输入值的形式相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!