symfony 2, collection of forms, validation error -
my form type has collection field:
$builder->add('affiliates', 'collection', array( 'type' => new affiliateformtype(), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, )); in template have:
<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> {% 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) adding/deleting rows is:
<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; (var = 1; < 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> let name required field. above code works fine except 1 case: when row added , deleted , added again, deleted index not available anymore first row's index=1, second row's index=3; , when invalid form submitted (for example name field empty) form.isvalid() correctly returns false, validation errors not shown beneath respective elements. can me correct issue? thank you.
have @ bernhard schussek's comment on here:
how can add violation collection?
you have explicitly set error_bubbling false ( defaults true ) collections prevent errors bubbling tree main form , being shown there global errors.
as ( far can see in question ) don't have {% form_errors(form) %} inside template global form errors aren't shown collection's errors should present global errors in form right now.
Comments
Post a Comment