2

多亏了这个帮助,我可以创建一个带有可变标签的表单。现在我想创建一个此类表单的集合,但我做不到。

准确地说:我有一个 MyField 类:

class MyField {
    protected label;
    protected userAnswer;

    public function setLabel($label){...}
    public function getLabel(){...}
    public function setUserAnswer($answer){...}
    public function getUserAnswer(){...}
}

我还有一个 MyFieldType 类:

class MyFieldType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $myField = $options['myField'];
        $builder->add('userAnswer', 'text', array('label' => $myField->getLabel()));
    }

    public function getDefaultOptions(array $options){
        return array('data_class' => '...\MyField',
                        'myField' => null);
    }
}

并且,在控制器中:

public function MyAction(Request $request){
    ...
    $myField = new MyField();
    ...
    $form = $this->createForme(new MyFieldType(), $myField, array('myField' => $myField));
    ...
}

综上所述,字段标签是 $myField->label。

现在我想用几个 MyFields 创建一个表单:我有一个 MyForm 类

class MyForm {
    protected myFields; // array of MyField
    public function setMyFields...
    public function getMyFields...
    public function addAField...
}

而且我仍然希望字段标签为 $myField->label。那么,我该如何定义 MyFormType ?
我尝试了类似的东西:

class MyFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options){
        $myFields = $options['myFields'];
        $builder->add('myFields', 'collection',
                array('type' => new MyFieldType(),
                    'options' => array('myField' => $myFields[0])));
    }

它可以工作(当然所有字段都有相同的标签,我不希望这样),但我不知道如何给每个 MyFieldType 自己的 MyField ...

任何想法 ?

4

2 回答 2

1

我会创建这两种类型:

class MyFormType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options){

        $builder->add('myFields', 'collection',
                array('type' => new MyFieldType());
    }




class MyFieldType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options){

        $builder->add('label', 'text');
    }

希望能帮助到你

于 2012-11-16T18:59:56.183 回答
0

我找到了一种方法来做我想做的事(感谢 nass600 的回答和我的回复):我创建了一种新型字段(参见http://symfony.com/en/doc/current/cookbook/form/create_custom_field_type .html ) 像这样:

class LabelType extends AbstractType{
    public function setDefaultOptions(OptionsResolverInterface $resolver){
    }

    public function getParent(){
        return 'text';
    }

    public function getName(){
        return 'label';
    }
}

然后,我改变了 MyFieldType :

class MyFieldType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('label', new LabelType())
                ->add('userAnswer', 'text');
    }

    public function getDefaultOptions(array $options){
        return array('data_class' => '...\MyField')
    }
}

我还创建了一个 fields.html.twig,就像 Symfony 食谱中解释的那样,只有:

{% block label_widget %}
  {{ value }}
{% endblock %}

而且,现在,在我的 myForm.html.twig 中:

...
{% for field in form.myFields %}
{% form_widget(field.label) %}
{% form_widget(field.userAnswer) %}
...

适用于:

class MyFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options){
        $myFields = $options['myFields'];
        $builder->add('myFields', 'collection',
                array('type' => new MyFieldType()));
    }
...

再见

于 2012-11-17T01:52:50.277 回答