Symfony 2 formset validation error

My form type has a collection field:

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

In the template, I:

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

Now javasript code (with jquery) for adding / removing lines:

 <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> 

Say a field name is required. The above code works fine, except for one case: when a row is added and deleted and added again, the deleted index is no longer available, as the index of the first row = 1, the second index of the row = 3; and when an invalid form is displayed (for example, the name field is empty) form.isValid () correctly returns false, but validation errors do not appear under their corresponding elements. Can someone help me fix the problem? Thanks.

+4
source share
1 answer

Take a look at Bernhard Schusseck here:

How to add violation to the collection?

You must explicitly set error_bubbling to false (default is true ) for collections to prevent errors in the tree from appearing in your main form and appear as global errors there.

Since you (as far as I see in your question) do not have {% form_errors(form) %} inside your template, these global form errors do not appear, but your collection errors should present global errors in your form right now.

+1
source

Source: https://habr.com/ru/post/1492066/


All Articles