【问题标题】:symfony 2, collection of forms, validation errorsymfony 2,表单集合,验证错误
【发布时间】:2013-07-18 11:07:24
【问题描述】:

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

$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();"/>

现在用于添加/删除行的 javasript 代码(带有 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,但验证错误不会显示在其各自元素下方。有人可以帮我纠正这个问题吗?谢谢。

【问题讨论】:

    标签: forms validation symfony


    【解决方案1】:

    在此处查看 Bernhard Schussek 的评论:

    How can I add a violation to a collection?

    您必须为集合显式设置error_bubblingfalse(默认为true),以防止错误冒泡到您的主窗体中并在那里显示为全局错误。

    正如您(据我在您的问题中看到的)在您的模板中没有{% form_errors(form) %},这些全局表单错误不会显示,但您的集合错误现在应该在表单中显示为全局错误。

    【讨论】:

    • 我按照您的建议设置了 error_bubbling = false,但问题仍然存在。如果 error_bubbling=true 并且 {{ form_errors(form) }} 包含在模板中,则验证错误将显示在全局范围内。 Symfony 似乎无法将这些错误与实际的集合条目字段相关联,但不知何故仍然设法得到错误。我很确定这与被跳过的索引有关。它发生在新行后面跟着另一个新行,并且它们的索引不按顺序排列(因为它们之间的一行被用户删除)。
    猜你喜欢
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    相关资源
    最近更新 更多