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 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题