douxianxing5712 2014-07-22 10:12
浏览 48
已采纳

Symfony返回表格字段

I would like to create a function which returns a form field that can then be added to a form. Currently I have something like:

$builder->add($name,$type,$options);

I would like something similar to:

function getField()
{
    //$name, $type, $options = blah

    $builder = $this->createFormBuilder();
    $builder->add($name,$type,$options);
    return $builder;
}

$field = getField();
$builder->add($field);
  • 写回答

1条回答 默认 最新

  • douya2982 2014-07-22 11:36
    关注

    You should only create one FormBuilder per Form. Your issue could be solved by passing the FormBuilder instance to the the function that generates the field:

    function addField($builder)
    {
        //$name, $type, $options = blah
    
        $builder->add($name,$type,$options);
    }
    
    $builder = $this->createFormBuilder();
    
    addField($builder);
    

    Does this meet your requirements?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?