表单主题 - 在n block_name匹配时添加代码

问题描述 投票:0回答:1

我有一个前面的symfony应用程序,它从API获取序列化的symfony表单,解析它并最终呈现它。

这个应用程序应该是愚蠢的,不应该以任何方式了解任何远程应用程序的逻辑。它只需要json格式,并在解析后显示它。

序列化表单中的字段具有自定义(远程应用程序定义)块名称,然后在前端应用程序的表单主题中使用这些名称来构建字段结构。

所述字段示例:

"field_1": {
    "options": {
        "block_name": "block_name_example",
        "label": "Example",
        "required": true,
        "disabled": false,
        "choices": {
            "Choice 1": "1",
            "Choice 2": "2"
        },
        "help_description": "",
        "attr": {
            "name": "field_name_1",
            "short_name": "fieldName1"
        }
    },
    "type": "Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType"
}

我想在form_theme块中添加“第一次匹配此块名称”(例如),而不在远程应用程序端添加任何逻辑,例如:

{% block _form_block_name_example %}
    {% if match_occurrence = 1 %}
        {# do something here #}
    {% endif %}
    {{ form_widget(form) }}
{% endblock %}

我知道有很多方法(表单字段额外选项,将其包装到集合类型字段......)通过远程应用程序代码编辑来解决这个问题,但我不希望出于各种原因,主要是避免任何远程应用程序代码的额外复杂性。

找不到一个干净的方法来解决这个问题。你会成为我的英雄吗?

forms symfony twig symfony4
1个回答
0
投票

我只是在一个非常干净的方式来处理这个在前端应用程序。从不相关的代码中剥离最大类并重命名一些东西,使其尽可能通用。

我通过表单扩展为我的字段添加了一个新的额外first_of_type选项,我在表单生成时设置了该选项。然后我将它用作我的表单主题块中的随意选项。代码如下。

表格扩展名:

class ExtraOptionsExtension extends AbstractTypeExtension
{
    /**
     * Add the width option.
     *
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefined([
            /* ... */
            'first_of_type'
        ]);
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        /* ... */
        $view->vars['first_of_type'] = false;
        if (!empty($options['first_of_type'])) {
            $view->vars['first_of_type'] = true;
        }
    }

    /**
     * Returns the name of the type being extended.
     *
     * @return string The name of the type being extended
     */
    public function getExtendedType()
    {
        return FormType::class;
    }
}

FormType:

abstract class AbstractType extends BaseAbstractType
{
    /* ... */

    /**
     * {@inheritdoc}
     *
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $blockNames = [];
        if (is_array($this->fields)) {
            foreach ($this->fields as $property => $data) {
                $type = $data['type'] ?? null;
                $opts = $data['options'] ?? [];
                if (!in_array($type, $this::TYPES_WITHOUT_EXTRA) &&
                    isset($data['options']['block_name']) &&
                    !in_array($data['options']['block_name'], $blockNames)) {
                    $blockNames[] = $data['options']['block_name'];
                    $opts['first_of_type'] = true;
                }
                $builder->add($property, $type, $opts);
            }
        }
    }

    /* ... */
}

表单主题块:

{% block custom_block %}
    <div class="form-group form-inline {% if not first_of_type %} hide {% else %}">
        {{ form_label(form, null, {'label_attr': {'class': 'control-label'}}) }}
        {{ form_widget(form) }}
    </div>
{% endblock %}

仍然可以进行改进,以提供有关元素位置的更多信息(不仅仅是它的第一个事实)。

© www.soinside.com 2019 - 2024. All rights reserved.