几乎所有内容都可以使用,但是我不知道为什么第二个include只能部分使用。
我解释自己=>
我有一个树枝模板,其中包含一个名为“ type”的表单行:
{% extends 'base.html.twig' %}
{% block title %}Supports pédagogiques - {{ parent() }}{% endblock %}
{% block body %}
<div class="container">
{{ form_start(form, { 'attr': {'novalidate': 'novalidate'}} ) }}
<div class="row justify-content-center">
<div class="col-md-10 alert alert-light text-center">
<h1>Nouveau contenu</h1>
<hr>
{{form_row((form.type), {'id': 'type'}) }}
<div class="form_container">
</div>
</div>
</div>
{{ form_end(form, {'render_rest': false}) }}
</div>
{% endblock %}
此行提供了3个“选项”供您选择,如您在ContentType.php中看到的那样:
<?php
namespace App\Form;
use App\Entity\Content;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ContentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', ChoiceType::class, array(
'placeholder' => 'Choisissez un type de contenu',
'label' => 'Type de contenu',
'choices' => array(
'Tutoriel vidéo' => 'Tutoriel',
'Autre vidéo' => 'Vidéo',
'Support PDF' => 'Support PDF'
),
))
->add('title', TextType::class, array(
'label' => 'Titre'
))
->add('link', UrlType::class, array(
'label' => 'Lien de la vidéo (copier l\'URL de la page)'
))
->add('description', TextType::class, array(
'label' => 'Description du contenu'
))
->add('pdfFile', VichFileType::class, array(
'label' => 'Uploader le PDF'
))
// ->add('created_at')
// ->add('user')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Content::class,
]);
}
}
并且我希望根据选择的选项来包含其余的表单(一种表单用于PDF,另一种表单用于Tutoriel和Video),所以我添加了一些Jquery:
{% block javascripts %}
<script>
$("#type").on('change', function () {
var type = $(this).val();
if (type == 'Support PDF') {
$('.form_container').html(`{% include 'form/pdf.html.twig' %}`);
} else {
$('.form_container').html(`{% include 'form/video.html.twig' %}`);
}
});
</script>
{% endblock %}
这是form / pdf.html.twig:
{{form_row(form.title)}}
{{form_row(form.description)}}
{{form_row(form.pdfFile)}}
<button type="submit" class="btn btn-success">Ajouter le PDF</button>
这是form / video.html.twig:
{{form_row(form.title)}}
{{form_row(form.description)}}
{{form_row(form.link)}}
<button type="submit" class="btn btn-success">Ajouter la vidéo</button>
当选择“支持PDF”时,一切都很好,包括了完整的表格。 但是,如果选择其他两个中的一个,则video.html.twig仅部分包含:
更准确地说:不包括{{form_row(form.title)}}和{{form_row(form.description)}}。
我只有:
{{form_row(form.pdfFile)}}
<button type="submit" class="btn btn-success">Ajouter le PDF</button>.
我无法解释这个问题...