Symfony 2.8.x:自定义FormType会覆盖表单变量

时间:2016-02-04 12:15:09

标签: forms symfony

我有一个包含多个字段的表单。 我还使用一些自定义表单类型,例如DivType来添加文本,按钮,图片或其他东西,而不是映射到数据库。

将代码升级到Symfony 2.8语法会导致无法实现的行为。

我的表格:

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

        // A couple of fields, for example field1
        $builder->add('field1', 'text', array(
            //  text field
        ));

        // A div element
        $builder->add('div_id_1', new DivType(), array(
            'content' => 'Just some content in the first div',
        ));

        // Another div
        $builder->add('div_id_2', new DivType(), array(
            'content' => 'A div with some images, buttons or other cool stuff',
        ));

    }
}

我的DivType.php:

class DivType extends AbstractType {

    private $content;
    private $id;

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $this->content = $options['content'];
        $this->id = $builder->getName();
    }

    public function buildView(FormView $view, FormInterface $form, array $options) {
        $view->vars['div_content'] = $this->content;
        $view->vars['div_id'] = $this->id;
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array (
            'mapped' => false,
            'content' => false,
        ));
        $resolver->setDefined(array('div_id'));
    }

    public function getName() {
        return 'div';
    }
    public function getBlockPrefix() {
        return 'div';
    }

}

我的模板代码:

{% block div_row %}
    <div id="{{ div_id }}">
    {% if div_content is defined and div_content %}
        {{ div_content }}
    {% endif %}
    </div>
{% endblock %}

div的结果(到目前为止工作正常: - ):

...
<div id="div_id_1">
    Just some content in the first div
</div>
<div id="div_id_2">
    A div with some images, buttons or other cool stuff
</div>
...

现在我升级到symfony 2.8:
在分析器中的弃用消息中,此行显示: “将类型实例传递给FormBuilder :: add(),Form :: add()或FormFactory从版本2.8开始不推荐使用,3.0版本不支持。请使用完全限定类型的类名称(MyBundle \ Form \ Type \ DivType)“

所以我将ExampleType类中的语法更改为新语法(DivType :: class):

$builder->add('div_id_1', DivType::class, array(
    'content' => 'Just some content in the first div',
));
$builder->add('div_id_2', DivType::class, array(
    'content' => 'A div with some images, buttons or other cool stuff',
));

现在结果不是我所期待的。 我得到了添加到表单中的div的数量,但是所有都被最后添加的元素的值覆盖,这显然不是很有用...

<div id="div_id_2">
    A div with some images, buttons or other cool stuff
</div>
<div id="div_id_2">
    A div with some images, buttons or other cool stuff
</div>

有人知道我做错了什么吗? 或者我可以使用另一种方法来添加自定义表单类型吗?

1 个答案:

答案 0 :(得分:1)

感谢Cerad,我能够解决问题! 显然我太复杂了......使用buildView方法中的$ options并从buildForm方法中删除它们确实可以完成这项工作。

我的新DivType:

if ($(('.test').val() >= 1) && (('.test').val() <= 100)))

也许更好更清洁的方法是直接在树枝中使用element-id,使用变量&#39; form.vars.name&#39;对于ID,而不是添加额外的变量&#39; div_id&#39;:

class DivType extends AbstractType {

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

    }

    public function buildView(FormView $view, FormInterface $form, array $options) {
        $view->vars['div_content'] = $options['content'];
        $view->vars['div_id'] = $options['div_id'];
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array (
            'mapped' => false,
            'content' => false,
        ));
        $resolver->setDefined(array('div_id'));
    }

    public function getBlockPrefix() {
        return 'div';
    }

}