$builder->add('affiliates','collection',array( 'type' => new AffiliateFormType(),'allow_add' => true,'allow_delete' => true,'by_reference' => false,));
在模板中我有:
<table ID="affiliates" > <tr> <th ></th> <th data-prototype="{{ form_Widget(form.affiliates.vars.prototype.affiliate_name)|e }}">name</th> <th data-prototype="{{ form_Widget(form.affiliates.vars.prototype.affiliate_type_code)|e }}">Type</th> <th data-prototype="{{ form_Widget(form.affiliates.vars.prototype.address)|e }}">Address</th> </tr> {% for affiliate in form.affiliates %} <tr> <td ><input type="button" value="Delete"/></td> <td >{{ form_Widget(affiliate.affiliate_name) }}{{ form_errors(affiliate.affiliate_name) }}</td> <td >{{ form_Widget(affiliate.affiliate_type_code) }}{{ form_errors(affiliate.affiliate_type_code) }}</td> <td >{{ form_Widget(affiliate.address) }}{{ form_errors(affiliate.address) }}</td> </tr> {% endfor %}</table><input type="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" 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;当提交无效表单时(例如,name字段为空),form.isValID()正确返回false,但验证错误未显示在各自元素下方.有人可以帮我纠正这个问题吗?谢谢.
@H_403_4@解决方法 看看Bernhard Schussek在这里的评论:How can I add a violation to a collection?
您必须为集合显式设置error_bubbling为false(默认为true),以防止错误将树冒泡到主窗体中并在那里显示为全局错误.
正如您(在我的问题中可以看到的)在模板中没有{%form_errors(form)%}那些全局表单错误没有显示但是您的集合错误现在应该在表单中显示为全局错误.
@H_403_4@ @H_403_4@ @H_403_4@ @H_403_4@ 总结以上是内存溢出为你收集整理的symfony 2,表单集合,验证错误全部内容,希望文章能够帮你解决symfony 2,表单集合,验证错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)