duanbiyi7319 2015-02-13 18:22 采纳率: 100%
浏览 25
已采纳

将自定义值传递给Symfony表单中的每个输入(类型为选项)

I'm trying to figure out the best approach to this problem. I am querying a database to get a list of values which I am using to create a form field. In reality, though, it will be creating about 12 <input type="checkmark" /> tags. I need to provide an image URL, which is also from the database, to each checkmark (really the label associated with it).

This seems like it would be a piece of cake if I was rendering each <input /> individually by simply creating a Form Extension. I'm not building them individually, and it doesn't seem right to loop through an array (which Propel would provide) to build as many checkboxes as needed even if I was.

The Propel model form field type is easy to use, but will pass the image column data right into the label which is entirely unpractical. It makes the label into a string that's something like (exploding the string also doesn't seem a good option):

Label => "
    ID: 2
    Title: Label of Checkmark
    Parent ID: null
    Image: ek9dkB.jpeg" 

So what would the best way to pass a parameter to each child so I can access it in my fields.html.twig?

Relevant Form Builder (PictureCheckbox is my custom field type so it will render a custom Twig form template):

$builder->add('skills', new PictureCheckbox(), array(
      'choices'     => $choices,
      'required'    => true,
      'label'       => 'What are your skills?',
      'multiple'    => true,
      'expanded'    => true,
    ));

I've used the model type as well above, which is very similar in style, but the idea is the same.

Here's the field.html.twig where I've created a new form type. {% block picture_checkbox_widget %}

    {% if expanded %}
        {% for child in form.children %}
            {% spaceless %}
            <label for="{{ id }}">
                {# Most of this would change, but is here for example: #}
                {{ form_label(child.label) }}
                    {% set file = path.from.child %}
                    <img src="{{ asset('uploads' ~ file) }}" alt="Test Image" />
            </label>
            {% endspaceless %}
        {% endfor %}
            <input type="checkbox" id="{{ id }}"{{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    {% endif %}
{% endblock %}

I am using Symfony 2(.6) with Propel as the ORM. I'd be happy to add info, if I missed something relevant.

Perhaps the choice_list is my answer? But it seems there would be a way to add a variable to the list I see when I dump the child: {% dump(child) %}.

  • 写回答

1条回答 默认 最新

  • dongliang_bj2016 2015-02-13 20:57
    关注

    I believe I have found the answer. I built a ChoiceList and added the image and title to that. I'm sure there is a better way, but it seems to work well, and seems logical from the standpoint that the image is in the <label /> anyway so delivering it as part of the label is natural.

    I'll try to update with specifics when I have more time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程