Symfony 2,表单集合,验证错误

3

我的表单类型有一个集合字段:

$builder->add('affiliates', 'collection', array(
    'type' => new AffiliateFormType(),
    'allow_add' => true, 
    'allow_delete' => true, 
    'by_reference' => false,
));

在模板中,我有以下内容:
<table id="affiliates" >
    <tr>
        <th class="t1c0"></th>
        <th class="t1c1" data-prototype="{{ form_widget(form.affiliates.vars.prototype.affiliate_name)|e }}">Name</th>
        <th class="t1c2" data-prototype="{{ form_widget(form.affiliates.vars.prototype.affiliate_type_code)|e }}">Type</th>
        <th class="t1c3" data-prototype="{{ form_widget(form.affiliates.vars.prototype.address)|e }}">Address</th>
    </tr>
    {% for affiliate in form.affiliates %}
    <tr>
        <td class="t1c0"><input type="button" class="delete_button" value="Delete"/></td>
        <td class="t1c1">{{ form_widget(affiliate.affiliate_name) }}{{ form_errors(affiliate.affiliate_name) }}</td>
        <td class="t1c2">{{ form_widget(affiliate.affiliate_type_code) }}{{ form_errors(affiliate.affiliate_type_code) }}</td>
        <td class="t1c3">{{ form_widget(affiliate.address) }}{{ form_errors(affiliate.address) }}</td>
    </tr>
    {% endfor %}
</table>
<input type="button" class="add_button" value="Add line" onclick="addAffiliate();"/>

现在添加/删除行的JavaScript代码(使用jQuery)如下:

<script language="javascript">

    var affiliatesCollection = $('table#affiliates');

    $(document).ready(function(){
        var rowCount = $('table#affiliates tr').length;
        affiliatesCollection.data('index', rowCount - 1);

        $('.delete_button').click(function(e) {
            $(this).closest("tr").remove();
        });
    });

    function addAffiliate() {
        //get index
        var index = affiliatesCollection.data('index');
        affiliatesCollection.data('index', index + 1);

        //add row
        var cells = new Array();
        var cell = $('<input type="button" class="delete_button" value="Delete"/>').click(function (){
            $(this).closest("tr").remove();
        });
        var $cell = $('<td></td>').append(cell);
        cells[0] = $cell;

        for (var i = 1; i < 4; i++)
        { 
            var prototype = $('th.t1c'.concat(i)).data('prototype');
            var cell = prototype.replace(/__name__/g, index);
            var $cell = $('<td></td>').append(cell);
            cells[i] = $cell;
        }
        var $newRow = $('<tr></tr>').append(cells);
        affiliatesCollection.append($newRow);
    }

</script>

假设名称是必填字段。上面的代码运行良好,除了一个情况:当添加一行并删除后再次添加时,删除的索引不再可用,例如第一行的索引为1,第二行的索引为3;当提交无效表单(例如名称字段为空)时,form.isValid() 正确地返回 false,但验证错误未显示在其各自元素下方。有人能帮我纠正这个问题吗?谢谢。
1个回答

1

请看Bernhard Schussek在这里的评论:

如何向集合中添加违规项?

您必须明确将error_bubbling设置为false(默认为true),以防止错误冒泡到主表单并在那里显示为全局错误。

因为您(就我所看到的问题而言)没有在模板中使用{% form_errors(form) %},所以这些全局表单错误不会显示出来,但是您的集合错误应该作为全局错误出现在您的表单中。


2
我按照你的建议将error_bubbling设置为false,但问题仍然存在。如果error_bubbling=true,并且模板中包含{{ form_errors(form) }},则验证错误会显示在全局范围内。Symfony似乎无法将这些错误与实际的集合条目字段关联起来,但仍然成功获取了这些错误。我相当确定这与索引被跳过有关。当新行后跟另一新行时,它们的索引不连续(因为用户删除了它们之间的一行)时就会出现此问题。 - synergetic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接