forms - 交响乐 2.8.x : Custom FormType overwrites form variables

标签 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:
在探查器的弃用消息中,此行显示: “自版本 2.8 起,不推荐将类型实例传递给 FormBuilder::add()、Form::add() 或 FormFactory,并且在 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>

有人知道我做错了什么吗?
或者是否有其他方法可以用来添加自定义表单类型?

最佳答案

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

我的新 DivType:

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';
    }

}

也许更好、更简洁的方法是直接在 twig 中使用 element-id,使用变量“form.vars.name”作为 id,而不是添加额外的变量“div_id”:

<div id="{{ form.vars.name }}">

关于forms - 交响乐 2.8.x : Custom FormType overwrites form variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200771/

相关文章:

php - 如何检查未登录用户是否具有角色?

mysql - symfony 4 中的独立数据库

javascript - 在同一页面上创建多个 redux-form 并使用相同的按钮提交

php - 当且仅当数组中的所有值都为真时才返回真?

forms - 为什么我的表单没有在 symfony 4 中提交?

php - Sonata sonata_type_model_list 显示对象名称

php - 使用 YAML 替换较小的 SQL 表?

javascript - 传递查询字符串后自动在不同页面提交表单?

javascript - Mailchimp 在一页上嵌入两个表单

php - Symfony2 : Getting Route in Page Load Event Listener