2

对于构建选择类型的 symfony 2 表单,我将不胜感激。我曾尝试寻找答案并跟踪了几条线索,但找不到我正在寻找的确切内容。

下面的 QuestionType 类使用四个值(旨在用作表单中的选项)进行实例化。但是,在呈现表单时,选择不会反映已传递到类中的值。知道出了什么问题吗?提前致谢!

class QuestionType extends AbstractType
{   
    private $foptionone;
    private $foptiontwo;
    private $foptionthree;
    private $foptionfour;

    public function buildForm(FormBuilderInterface $builder, array $options)
    {   
        $optionone=$this->foptionone;
        $optiontwo=$this->foptiontwo;
        $optionthree=$this->foptionthree;
        $optionfour=$this->foptionfour;

        $builder->add('sanswer','choice',array(
            'expanded'=>true,
            'choices'=>array(
                'a'=>$optionone,
                'b'=>$optiontwo,
                'c'=>$optionthree,
                'd'=>$optionfour)
            )
        );
    }

    public function _construct($qoptionone,$qoptiontwo,$qoptionthree,$qoptionfour)
    {
        $this->foptionone=$qoptionone;
        $this->foptiontwo=$qoptiontwo;
        $this->foptionthree=$qoptionthree;
        $this->foptionfour=$qoptionfour; 
    }

    public function getName()
    {
        return 'questiontype';
    }
4

1 回答 1

0

构造函数有两个下划线,而不是一个:

public function __construct(...)

您的_construct()方法根本不会被调用。

于 2013-06-13T18:12:10.380 回答