在php中访问表单的成员

时间:2015-07-01 11:42:05

标签: php forms symfony twig symfony-forms

我有以下代码:

$form_logistique = new Form\Form();
    foreach($aRefArticle as $k => $v) {
        $form_logistique->add(
            new Form\Field\Text('ref_article_'.$k, $v, true),
            new Form\Field\Checkbox('availability_'.$k, true, false)
        );
    }
    $this->form_logistique = $form_logistique;

    if ($this->getRequest()->isPostMethod() && $this->form_logistique->bind($_POST) ) {
        foreach($aRefArticle as $k => $v) {
            $aPrices[]           = $this->form_logistique->ref_article_.$k->getValue();
        }
    }

表单已发送并显示在模板上。问题是,当我提交表单时,我会在此行中收到错误:

  

$这 - > form_logistique-> ref_article _ $ K->的getValue()

错误:

  

在非对象

上调用成员函数getValue()

我重复了模板上显示的表格。

2 个答案:

答案 0 :(得分:0)

$k是数组元素的关键,而不是元素。

你应该使用$v,假设那是你想要的对象。

编辑:

如果您需要引用密钥,请使用:

        $aPrices[] = $this->form_logistique->ref_article_.$aRefArticle[$k]->getValue();

答案 1 :(得分:0)

您应该尝试此操作来访问表单的字段:

$this->form_logistique->get("ref_article_$k")->getValue()