我在表单中添加了一个按钮添加部分。当用户单击该按钮时,它会打开一个提示窗口。提示窗口询问段ID之后,它进行ajax调用以从数据库获取段数据并创建具有相应字段的段表单然后我将该子表单(即段表单)附加到现有表单。但是当我提交表单时,symfony会抛出错误,表单无效。因此,基本上symfony表单不接受附加到现有表单的新创建的子表单
这就是我在symfony controller
中向表单添加按钮的方法 $formBuilder-> add('new_sections','hidden',array())
->add('add','button', array(
'label' => 'Add Section',
))
在twig文件中,Jquery将新字段附加到按钮上的现有表单单击
$('#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 ='' +
'<div style="border-style: solid; margin-top: 15px; border-radius: 3px; border-width: 1px; padding: 10px;">'+
'<label class="required">'+result['section']['section_id']+'</label>'+
'<div id="form_'+result['section']['section_id']+'" class="SectionForms">'+
' <div><label for="form_'+result['section']['section_id']+'_weight'+result['section']['section_id']+'">Weight </label><input type="text" id="form_'+result['section']['section_id']+'_weight'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][weight'+result['section']['section_id']+']" value="0"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_label'+result['section']['section_id']+'" class="required">label </label><input type="text" id="form_'+result['section']['section_id']+'_label'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][label'+result['section']['section_id']+']" required="required" value="Text Description v2"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_active'+result['section']['section_id']+'" class="required">Active </label><input type="text" id="form_'+result['section']['section_id']+'_active'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][active'+result['section']['section_id']+']" required="required" value="1"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_link'+result['section']['section_id']+'">Link </label><input type="text" id="form_'+result['section']['section_id']+'_link'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][link'+result['section']['section_id']+']"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_events_max'+result['section']['section_id']+'">events_max </label><input type="text" id="form_'+result['section']['section_id']+'_events_max'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][events_max'+result['section']['section_id']+']"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_events_min'+result['section']['section_id']+'">events_min </label><input type="text" id="form_'+result['section']['section_id']+'_events_min'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][events_min'+result['section']['section_id']+']"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_module'+result['section']['section_id']+'">module</label><input type="text" id="form_'+result['section']['section_id']+'_module'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][module'+result['section']['section_id']+']"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_module_icon'+result['section']['section_id']+'">module_icon </label><input type="text" id="form_'+result['section']['section_id']+'_module_icon'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][module_icon'+result['section']['section_id']+']" value="icon_generic"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_module_id'+result['section']['section_id']+'">module_id </label><input type="text" id="form_'+result['section']['section_id']+'_module_id'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][module_id'+result['section']['section_id']+']"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_apicall'+result['section']['section_id']+'">apicall </label><input type="text" id="form_'+result['section']['section_id']+'_apicall'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][apicall'+result['section']['section_id']+']" value="1"></div>'+
'<div><label for="form_'+result['section']['section_id']+'_template_static'+result['section']['section_id']+'">template_static </label><input type="text" id="form_'+result['section']['section_id']+'_template_static'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][template_static'+result['section']['section_id']+']"></div>'+
'<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">'+
' <div><button type="button" id="form_'+result['section']['section_id']+'_delete_'+result['section']['section_id']+'" name="form['+result['section']['section_id']+'][delete_'+result['section']['section_id']+']" class="btn-delete">Delete Section No. '+result['section']['section_id']+'</button></div>'+
'</div>'+
'</div>'
$('.SectionForms').last().parent().after(new_section_form);
$('#form_new_sections').val($('#form_new_sections').val()+" "+result['section']['section_id']);
}});
});
但是当我提交表格时,它没有以有效格式输入
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
// not entering in this block
}
}
答案 0 :(得分:0)