我正在编写带有集合类型的symfony2表单。表单有一个字段日期,用户填写日期并按下搜索按钮,jquery提交表单,并使用响应和选择日期的寄存器刷新html代码。我的问题是表单使用用户选择日期的学说的查询寄存器数量很好地呈现输入,但是都是空的而不加载数据。
我有两个事件PRE_SET_DATA和PRE_SUBMIT。 我尝试了一个基本的例子,但结果相同。 谢谢和问候。
表格:
$builder->add('description');
$builder->add('fecha', DateTimeType::class,array(
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'dd-MM-yyyy H:m',
'html5' => false,
'label' => "Fecha y hora"
));
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$asistencia = $event->getData();
if(!is_null($asistencia->getFecha())){
$form->add('tags', CollectionType::class, array(
'entry_type' => AsistenciaRegistrosTiempoType::class,
));
}else {
$form->add('tags', CollectionType::class, array(
'entry_type' => AsistenciaRegistrosTiempoType::class
));
}
});
$builder->get('fecha')->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
$form = $event->getForm()->getParent();
// this would be your entity, i.e. SportMeetup
$data = $event->getData();
$asistencia = $form->getData();
if(!is_null($data)){
$fecha = new \DateTime($data);
$array = new ArrayCollection();
$tag1 = new Tag();
$tag1->setName('tag1 '.$fecha->format("Y-m-d"));
$array->add($tag1);
$form->add('tags', CollectionType::class, array(
'entry_type' => AsistenciaRegistrosTiempoType::class,
'data'=> $array
));
} else {
$form->add('tags', CollectionType::class, array(
'entry_type' => AsistenciaRegistrosTiempoType::class,
));
}
});
}
表格与收藏:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name','text');
}
第一种形式的类:
class Asistencia
{
private $fecha;
protected $description;
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* @return mixed
*/
public function getFecha()
{
return $this->fecha;
}
/**
* @param mixed $fecha
*/
public function setFecha($fecha)
{
$this->fecha = $fecha;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getTags()
{
return $this->tags;
}
public function addTag($tag){
$this->tags->add($tag);
return $this;
}
public function setTags($tag){
$this->tags = $tag;
return $this;
}
}
第二种形式的类:
class Tag
{
private $name;
/**
* @return mixed
*/
public function getName()
{
ladybug_dump("Get ".$this->name);
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
ladybug_dump("Set ".$this->name);
$this->name = $name;
return $this;
}
}
动作:
$formAsistenciaTiempo = $this->createForm(
new AsistenciaType(),
$asistenciaTiempo,
array(
'action' => $this->generateUrl('dashboard'),
'method' => 'POST',
));
$formAsistenciaTiempo->handleRequest($request);
//if ($formAsistenciaTiempo->isSubmitted()) {
$tab = 2;
if ($formAsistenciaTiempo->isValid()) {
.......
嫩枝:
{{ form_start(formAsistencia) }}
{# render the task's only field: description #}
{{ form_row(formAsistencia.description) }}
<div class="col-md-6">
<div class="form-group">
{{ form_label(formAsistencia.fecha) }}
<div class="input-group date" id='fechahora'>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
{{ form_widget(formAsistencia.fecha) }}
<div class="input-group-btn">
<button type="button" class="btn btn-info" id="buscar_registros"><i class="fa fa-search"></i> Obtener registros</button>
</div>
</div>
</div>
</div>
<h3>Tags</h3>
<ul class="tags">
{# iterate over each existing tag and render its only field: name #}
{{ formAsistencia.tags | length }}
{% for tag in formAsistencia.tags %}
{{ tag |ladybug_dump }}
{#<li>{{ form_row(tag.name) }}</li>#}
{% endfor %}
</ul>
{{ form_rest(formAsistencia) }}
{{ form_end(formAsistencia) }}
<script>
var $sport = $('#asistencia_fecha');
// When sport gets selected ...
$("#buscar_registros").click(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('.tags').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('.tags')
);
// Position field now displays the appropriate positions.
}
});
});
</script>