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);

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?