我需要创建一个自定义表单,我已经用它的树枝模板创建了它,我很好地展示了它,我的问题是提交时,我不知道它发送到什么方法,我需要收集数据来通过它和将其保存在您的表中。
主要思想是制定时间表并动态添加输入,将其添加到 input [] 中,然后在函数中处理此数据并将其保存在表中。
控制器。
<?php
namespace App\Controller\Admin;
use...
class HorariosCrudController extends AbstractCrudController
{
public function configureCrud(Crud $crud): Crud
{
return $crud
->overrideTemplates([
'crud/edit' => 'admin/horarios/edit.html.twig',
'crud/new' => 'admin/horarios/new.html.twig',
])
;
}
public static function getEntityFqcn(): string
{
return Horarios::class;
}
}
看法:
{# templates/bundles/EasyAdminBundle/layout.html.twig #}
{# DO THIS: the '!' symbol tells Symfony to extend from the original template #}
{% extends '@!EasyAdmin/layout.html.twig' %}
{% block body_javascript %}
<script type="text/javascript">
$( document ).ready(function() {
var maxField = 9999; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div>' +
'<input type="time" name="field_name[\'lunes\']" value=""/>' +
'<input type="time" name="field_name[\'lunes\']" value=""/>' +
'<a href="javascript:void(0);" class="remove_button">' +
'<img src="remove-icon.png"/>' +
'</a>' +
'</div>';
//New input field html
var x = 1; //Initial field counter is 1
//Once add button is clicked
$(addButton).click(function(){
//Check maximum number of input fields
if(x < maxField){
x++; //Increment field counter
$(wrapper).append(fieldHTML); //Add field html
}
});
//Once remove button is clicked
$(wrapper).on('click', '.remove_button', function(e){
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
{% endblock %}
{% block main %}
<form>
<div class="field_wrapper">
<div>
<input type="time" name="field_name['lunes']" value=""/>
<input type="time" name="field_name['lunes']" value=""/>
<a href="javascript:void(0);" class="add_button" title="Add field"><img src="add-icon.png"/></a>
</div>
</div>
<button type="submit" class="btn btn-primary">Añadir</button>
</form>
{% endblock %}
当我提交时,我返回了一个 302