【问题标题】:Symfony2 static form collection labelsSymfony2 静态表单集合标签
【发布时间】:2014-07-07 19:11:57
【问题描述】:

我正在查看这部分文档:

http://symfony.com/doc/current/cookbook/form/form_collections.html

除了在我的情况下,每个tag 都是question,每个问题都有一个唯一的标签。

如何为集合表单创建唯一标签?

问题类型:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class QuestionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Answer', 'choice' array(
        'choices' => array(
                  '' => 'select one',
                  'yes',
                  'no')
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\AcmeBundle\Entity\Question\Question',
        ));
    }

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

问题集:

class BriefQuestionaireType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Questions', 'collection', array(
            'type' => new QuestionType()
            )
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\AcmeBundle\Entity\Question\Questionaire',
        ));
    }

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

我希望能够做类似的事情:

$builder->add('Questions', 'collection', array(
                'label' => 'Q1:Have you ever...?', //I'd like to use unique but static questions so that I can reuse the questions again later.`
                'type' => new QuestionType()
                )
            );


$builder->add('Questions', 'collection', array(
                'label' => 'Q2:Have you also...?', //I'd like to use unique but static questions so that I can reuse the questions again later.`
                'type' => new QuestionType()
                )
            );

但是上面的内容会覆盖之前的标签。这样只会出现标签Q2:Have you also...?

希望现在更清楚了,这就是我说我想要具有是/否选择选项的独特标签(只是静态问题)时的意思。

【问题讨论】:

  • 集合的唯一标签是什么意思?就像集合的第一个元素获得标签“第一个标签”,第二个元素获得标签“其他标签”,... aso ?
  • 编辑了我的问题以获取更多信息。

标签: forms symfony


【解决方案1】:

如果我对你想要达到的目标是正确的,我一周前已经回答了类似的问题:

How to pass entity atribute value to form Symfony2?

基本上,解决方案依赖FormEvents 将数据传送到Form动态地构造它的字段。

【讨论】:

  • 非常接近!我实际上想要一个集合中的静态标签,以便我能够在许多地方一遍又一遍地重用表单。编辑了我的问题以获取更多信息。
【解决方案2】:

您只需要一个集合字段。绑定到表单的数据中的“questions”数组将决定显示多少 QuestionType 字段。比如……

// Acme/DemoBundle/Controller/DefaultController.php

// ...

$data = array(
    'Questions' => array(
        array('Answer' => 'First Answer'),
        array('Answer' => 'Second Answer'),
        array('Answer' => 'Third Answer')
    );
);

$form = $this->createForm(new BriefQuestionaireType(), $data);

// ...

那么问题是,当所有问题的选项完全相同时,如何为每个问题字段显示唯一标签?

前几天我遇到了这个问题,就这样解决了。

// Acme/DemoBundle/Form/LabelGenerator.php    

class LabelGenerator{

    private $labels;

    public function __construct(array $labels){
        $this->labels = $labels;
    }        

    public function __toString(){
        $keyValue = each($this->labels);
        return $keyValue['value'];
    }

}

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php

// ...

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

    $labelGenerator = new LabelGenerator(array(
        'Q1: What is the first question?',
        'Q2: What is the second question?',
        'Q3: What is the third question?'
    ));

    $builder->add('Questions', 'collection', array(
        'type' => new QuestionType(),
        'options' => array(
            'label' => $labelGenerator    
        )
    ));

}

// ...

每当表单主题呈现一个 Question 标签时,LabelGenerator 都会返回数组中的下一个值。

我建议您使用 setDefaultOptions() 在 BriefQuestionaireType 表单中添加“question_labels”选项。然后,您可以像这样将它们传递给集合。

// Acme/DemoBundle/Controller/DefaultController.php

// ...

$form = $this->createForm(new BriefQuestionaireType(), $data, array(
    'question_labels' => $labels
));

// ...

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php

// ...

public function setDefaultOptions(OptionsResolverInterface $resolver) {

    $resolver->setRequired(array('question_labels'));

}

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

    $labelGenerator = new LabelGenerator($options['question_labels']);

    $builder->add('Questions', 'collection', array(
        'type' => new QuestionType(),
        'options' => array(
            'label' => $labelGenerator    
        )
    ));

}

// ...

或者,如果您想从问题实体中获取标签,您可以这样做。

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php

// ...

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

    $labels = array();
    foreach($builder->getData()->getQuestions() as $question){
        $labels[] = $question->getLabel();
    }

    $labelGenerator = new LabelGenerator($labels);

    $builder->add('questions', 'collection', array(
        'type' => new QuestionType(),
        'options' => array(
            'label' => $labelGenerator    
        )
    ));

}

// ...

编辑:您需要使用适当数量的问题实体预先填写问题,否则您将只有一个空集合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多