Symfony2 Form becomes invalid on submit when I am trying to append new elements in form

Tuesday, November 3, 2015

I have added a button Add section in the form. when a user click that button it opens a prompt window. Prompt window ask for section id After that it makes ajax call to get the section data from database and creates section form with the respective fields Then I am appending that subform(i.e section form) to the existing form. But when I am submitting the form symfony is throwing error that form is invalid. So basically symfony form does not accept the newly created subform appended to existing form



This is how I am adding button to the form in symfony controller



 $formBuilder-> add('new_sections','hidden',array())
->add('add','button', array(
'label' => 'Add Section',

))


in twig file, Jquery is appending the new fields to the existing form on button Click



 $('#form_add').click(
function(){
var section_id = prompt("Please enter your Section ID");
console.log(section_id);
$.ajax({url: url, success: function(result){

var new_section_form ='' +
'
'+
''+result['section']['section_id']+''+
'
'+
'
Weight
'+
'
label
'+
'
Active
'+
'
Link
'+
'
events_max
'+
'
events_min
'+
'
module
'+
'
module_icon
'+
'
module_id
'+
'
apicall
'+
'
template_static
'+

'<input type="hidden" id="form_'+result['section']['section_id']+'_changed'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][changed'+result['section']['section_id']+']" class="changedField" value="2">'+
'
Delete Section No. '+result['section']['section_id']+'
'+
'</div>'+
'</div>'


$('.SectionForms').last().parent().after(new_section_form);
$('#form_new_sections').val($('#form_new_sections').val()+" "+result['section']['section_id']);

}});
});


But when I am submitting the form its not entering in form valid block



 if ('POST' === $request->getMethod()) {

$form->handleRequest($request);

if ($form->isValid()) {
// not entering in this block
}
}

0 comments:

Post a Comment