Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2带选项的无类表单:检索提交的数据_Php_Forms_Symfony - Fatal编程技术网

Php Symfony2带选项的无类表单:检索提交的数据

Php Symfony2带选项的无类表单:检索提交的数据,php,forms,symfony,Php,Forms,Symfony,我在使用symfony2和带有选择字段的无类表单时遇到问题 设置非常非常简单: $data=array(); $choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing'); $fb=$this->createFormBuilder($data); $fb->add('mythings', 'choice', array( 'choices' =&

我在使用symfony2和带有选择字段的无类表单时遇到问题

设置非常非常简单:

$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');

$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
        'choices' => $choices,
        'data' => $data,
        'required' => false,
        'expanded' => true, 
        'multiple' => true,
        'mapped' => false))->
    add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
然后

$request=$this->getRequest();
$form->handleRequest($request);
if($form->isValid())
{
    //Go nuts.
}
这是在“发疯”的部分,我被卡住了。我实际上需要通过$choices数组中给出的原始索引来访问选定的选项。。。但我一直没能做到。这是我得到的最接近的结果:

$submitted_data=$form->get('mythings'); 
foreach($submitted_data as $key => $value) 
{   
echo $key.' -> '.$value->getData().'<br />'; 
}
这是绝对必要的,这是一个无类的形式。如何访问选项的原始索引(在本例中为1、2或100)


非常感谢。

这是字段映射的问题。传递到表单中的数据需要应用到要添加的字段(“mythings”),因此必须将$data包装在数组中,作为键“mythings”的值

例如:

$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');

$fb=$this->createFormBuilder(array('mythings' => $data));
$fb->add('mythings', 'choice', array(
    'choices' => $choices,
    'required' => false,
    'expanded' => true, 
    'multiple' => true))
   ->add('save', 'submit', array('label' => 'Save'));

$form=$fb->getForm();

您可以尝试命名数组键以保留实际索引

$data=array();
$choices=array('1' => 'A thing', '2' => 'This other thing', '100' => 'That other thing');

$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
        'choices' => $choices,
        'data' => $data,
        'required' => false,
        'expanded' => true, 
        'multiple' => true,
        'mapped' => false))->
    add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
将为您提供所需的数据


我希望这能有所帮助。

嗯。。。我没有这样做。办公时间结束了。我明天会好好看看的。尽管如此,还是非常感谢。仅供参考:这很奇怪。。。正常情况下,这应该可以完成你可以看到的工作是的,我认为我在实际的生产代码中做了一个糟糕的设置。根据SA Alex的回答,我认为传递给无类表单的数据数组决定了数据的“结构”。真正的生产代码有点不同。我明天会调查的。非常感谢。我尝试了类似的方法,根据复选框是否被选中,得到了“1”或null。你可以在原来的帖子里看到,只有一个foreach。我对symfony还比较陌生,所以一切都有点粗略。无论如何,谢谢。它给出了以下格式的数组:数组(0=>1,2=>100);把它扔掉以获取更多细节好吧我好像错过了第二个getData电话。。。我尝试了$form->get->('mythings'),然后尝试迭代它,并对每个项目调用get->Data()。你的方法真的起作用了。接受答案。非常感谢:)。
$data=array();
$choices=array('1' => 'A thing', '2' => 'This other thing', '100' => 'That other thing');

$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
        'choices' => $choices,
        'data' => $data,
        'required' => false,
        'expanded' => true, 
        'multiple' => true,
        'mapped' => false))->
    add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
$form->get('mythings')->getData()