如何在Symfony 4表单上的多个复选框周围添加包装器

时间:2019-05-15 15:17:56

标签: forms symfony twig

我有一个用以下形式创建的复选框列表

DELIMITER $$
 DROP PROCEDURE IF EXISTS test_mysql_while_loop$$
 CREATE PROCEDURE test_mysql_while_loop()
 BEGIN
 DECLARE x  INT;

 SET x = 100;

 WHILE x  <= 2000 DO

   DROP TABLE if EXISTS tablex; /* want to end up with table100-table2000 */

   CREATE TABLE tablex AS (
   SELECT t1.*
    FROM bigtables t1
    WHERE urn BETWEEN x AND x+101); /* select records where the URN is between 100 and 201 in 1st instance , 200 and 301 in second etc*/

    SET x=x+100;

 END WHILE;

 END$$
DELIMITER ;

CALL test_mysql_while_loop();

然后我可以使用以下命令将其显示在树枝文件中:->add('ISPreNbStudents', ChoiceType::class, [ 'multiple' => false, 'choices' => [ '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, ], 'expanded' => true ])

问题是它现在显示标签和输入的列表

{{ form_widget(form.ISOptMonths) }}

我需要像这样在每个标签/输入周围放置包装纸

<input type="radio" id="availability_ISPreNbStudents_0" name="availability[ISPreNbStudents]" value="1">
<label for="availability_ISPreNbStudents_0">1</label> 
<input type="radio" id="availability_ISPreNbStudents_1" name="availability[ISPreNbStudents]" value="2">
<label for="availability_ISPreNbStudents_1">2</label>

我该如何实现?

2 个答案:

答案 0 :(得分:0)

您必须使用Symfony Customize Form Rendering

这样,您可以按字段访问表单,例如:

{{ form_start(form) }}
    <div class="my-custom-class-for-errors">
        {{ form_errors(form) }}
    </div>

    <div class="row">
        <div class="col">
            {{ form_row(form.task) }}
        </div>
        <div class="col" id="some-custom-id">
            {{ form_row(form.dueDate) }}
        </div>
    </div>
{{ form_end(form) }}

您还可以手动呈现字段:

<div class="styled-input-single">
{{ form_widget(form.name_of_form_element) }}
{{ form_label(form.name_of_form_element) }}
<!-- and for errors of this field -->
{{ form_errors(form.name_of_form_element) }}
</div>

要手动呈现选择框或选项框,请检查以下链接:
https://symfonycasts.com

答案 1 :(得分:0)

我终于找到了解决方法:

这是您打印列表的方式:

{{ form_row(registrationForm.firstRecourse) }}

现在您是否要为每个选项添加包装器:

<div class="row">
    {{ form_label(registrationForm.ISPreNbStudents) }}
    {% for checkbox in registrationForm.ISPreNbStudents.children %}
        <div class="styled-input-single">
            {{ form_widget(checkbox) }}
            {{ form_label(checkbox) }}
            {{ form_errors(checkbox) }}
        </div>
    {% endfor %}
</div>