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 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启