我想在表单提交后设置表单隐藏的datetime元素的值(unix时间戳) Symfony 2.7。
$starttimeStr=$form->get('meetingbundle_event[starttimeStr]');// here i intend to get user input for the date in string format
$dateObj = new \Datetime($starttimeStr);
$starttimeInt=$dateObj->getTimestamp();
$form->get('meetingbundle_event[starttimeInt]')->setData($starttimeInt); //here i want to set the datetime in decimal format但这不起作用,因为在火狐中使用Tools-> WebDeveloperExtension->Forms->DisplayFormDetails时,FormInterface:http://api.symfony.com/3.0/Symfony/Component/Form/FormInterface.html的get(string $name)不能接受nor id meetingbundle_event_starttime和name meetingbundle_event[starttimeStr]作为有效参数。
因此,我尝试使用$elem=$form->all();查看表单元素的名称,但看不到结果:
print_r($elem); // crashes web-browser
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elem ); */ Complains that there is no memory
$elemser= serialize($elem); //gives error that php can not serialize Closure
$elemjson = json_encode($elem);
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elemjson ); // outputs empty strings表单域的默认命名规则是什么,以便我可以使用$form->get('field_name')检索它们
发布于 2015-12-16 03:50:35
要获取元素值,请使用$form->getData()['%element_name%']
据我所知,不可能通过调用$form->setData()来显式更改已提交的表单元素的值。因此,如果要在表单中更改已提交的表单元素值,请使用FormBuilder方法addViewTransformer http://symfony.com/doc/current/cookbook/form/data_transformers.html或在表单事件侦听器http://symfony.com/doc/current/components/form/form_events.html中执行此操作。
发布于 2015-12-16 03:30:45
从FormType类(不是字段id,也不是表单中的名称)中获取form的子级- use name:
$starttimeStr=$form->get('starttimeStr'); //where starttimeStr is the corresponding name of the field in Form class "\src\Bundle\Form\EventType.php"。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text', array( 'attr' => array( 'style' => 'width: 500px',), ) )
->add('keywords', 'text', array( 'attr' => array('style' => 'width: 500px',) ,) )
->add('starttime', 'hidden', array('data' => '0',) )
->add('endtime', 'hidden', array('data' => '0',) )
->add('starttimeStr') ...但是我仍然不能得到这个值。$starttimeStr似乎不是简单的输入日期字符串(21-12-2015),而是导致火狐崩溃的大型数组。
print_r($starttimeStr);// crashes Firefox.
$elemser=serialize('starttimeStr');
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elemser ); // outputs empty string "s:12:"starttimeStr";"发布于 2015-12-16 03:47:47
终于来了!一个人必须获取字段,然后才能获取数据。官方文档中没有对此进行描述。
$starttimeStr=$form->get('starttimeStr')->getData();
print_r($starttimeStr);//给用户输入"21-12-2015 04:33欧洲/伦敦“。
https://stackoverflow.com/questions/34297487
复制相似问题