我很小,在项目上与Symfony 2.8合作。 我在尝试访问一个我绝对不理解的复杂编辑表单时遇到此错误:
注意:未定义索引:\ TKS \ BlogBundle \ Form \ ArticleEditType
堆栈追踪:
- 在vendor \ symfony \ symfony \ src \ Symfony \ Component \ Form \ FormRegistry.php中 第103行
醇>@trigger_error(sprintf('通过其字符串名称访问类型“%s”是 自2.8版以来已弃用,将在3.0中删除。使用 完全限定的类型类名“%s”代替。',$ name, get_class($ this-> types [$ name] - > getInnerType())),E_USER_DEPRECATED); } 返回$ this-> types [$ name]; } / **
- 在ErrorHandler - > handleError('8','Undefined index:\ TKS \ BlogBundle \ Form \ ArticleEditType', 'C:\ XAMPP \ htdocs中\ tkstrs \厂商\ symfony的\ symfony的\ SRC \的Symfony \元器件\表格\ FormRegistry.php', '103',数组('name'=>'\ TKS \ BlogBundle \ Form \ ArticleEditType','type' => object(ArticleEditType),'extension'=> object(DependencyInjectionExtension))) vendor \ symfony \ symfony \ src \ Symfony \ Component \ Form \ FormRegistry.php at at 第103行(...)
醇>
我的编辑表单
<?php
namespace TKS\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Doctrine\ORM\EntityRepository;
use TKS\BlogBundle\Form\VilleContributionType;
class ArticleEditType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$this->idarticle = $options['idarticle'];
$idarticle = $this->idarticle;
$builder
->add('titre', TextType::class, array(
'label' => 'Titre',
'required' => true,
'attr' => array('class' => 'form-control widget-form')
))
->add('legende', TextType::class, array(
'label' => 'Légende',
'required' => true,
'attr' => array('class' => 'form-control widget-form')
))
->add('texte', TextareaType::class, array(
'label' => ' ',
'required' => false,
'attr' => array('class' => 'form-control widget-form')
))
->add('source', TextareaType::class, array(
'label' => ' ',
'required' => false,
'attr' => array('class' => 'form-control widget-form')
))
->add('lieu', TextType::class, array(
'label' => 'Nom du lieu',
'required' => true,
'attr' => array('class' => 'form-control widget-form')
))
->add('adresse', TextType::class, array(
'label' => 'Adresse',
'required' => true,
'attr' => array('class' => 'form-control widget-form')
))
->add('ville', EntityType::class, array(
'class' => 'TKSBlogBundle:Ville',
'expanded' => false,
'placeholder' => 'Choisissez une ville',
'empty_data' => null,
'multiple' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('v')
->orderBy('v.ville', 'ASC');
},
'attr' => array('class' => 'form-control widget-form'),
'label' => 'Ville',
'required' => false
))
->add('villeContribution', VilleContributionType::class, array(
'required' => false,
'mapped' => false,
'label' => ' '
))
->add('tags', EntityType::class, array(
'class' => 'TKSBlogBundle:Tag',
'expanded' => true,
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t')
->orderBy('t.nameFR', 'ASC');
},
'attr' => array('class' => ''),
'label' => 'Tags',
'required' => true
))
->add('tagsFree', EntityType::class, array(
'class' => 'TKSBlogBundle:TagFree',
'expanded' => true,
'multiple' => true,
'choice_label' => 'name',
'query_builder' => function (EntityRepository $er) use ($idarticle) {
return $er->findTagsFreeByArticle($idarticle);
},
'attr' => array('class' => ''),
'label' => ' ',
'required' => true
))
->add('tagsFreeNew', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => 'Nouveau tag',
'attr' => array('class' => 'form-control widget-form')
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'TKS\BlogBundle\Entity\Article',
'idarticle' => null
));
}
}
我的控制器:
/**
* Displays a form to edit an existing Article entity.
*
* @Route("/admin/editorial/{id}/edit", name="article_edit_editorial")
* @Method({"GET", "POST"})
*/
public function editEditorialAction(Request $request, Article $article) {
$idarticle = $article->getId();
$editForm = $this->createForm('\TKS\BlogBundle\Form\ArticleEditType', $article, array('idarticle' => $idarticle));
$editForm->handleRequest($request);
// récupération de la liste des tags
$tagsList = $editForm->get('tagsFreeNew')->getData();
if ($tagsList != null) {
// convertion de la liste en tableau
$tagsArray = explode(',', $tagsList);
$tagsArray = array_unique($tagsArray); // supprime les doublons dans un tableau
}
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
if ($tagsList != null) {
// on boucle sur le tableau de tags
for ($i = 0; $i < count($tagsArray); $i++) {
$tagName = $tagsArray[$i];
$tagName = trim($tagName);
// on vérifie l'existence du tag. Si oui->récupération...
$tagFreeEntity = $em->getRepository('TKSBlogBundle:TagFree')->findOneByName($tagName);
$tagEntity = $em->getRepository('TKSBlogBundle:Tag')->findOneByNameFR($tagName);
// si non, instanciation
if ($tagFreeEntity == null && $tagEntity == null) {
$tagFreeEntity = new \TKS\BlogBundle\Entity\TagFree();
$tagFreeEntity->setName($tagName);
// on ajoute le tag à l'article
$article->addTagsFree($tagFreeEntity);
}
// si tag déjà existant mais pas présent dans l'article , ajout du tag à l'article
if ($tagFreeEntity == null && $tagEntity != null) {
if (!in_array($tagEntity, $article->getTags())) {
// on ajoute le tag à l'article
$article->addTag($tagEntity);
}
}
// si tagFree déjà existant mais pas présent dans l'article, ajout du tagFree à l'article
if ($tagFreeEntity != null) {
if ($tagFreeEntity == $article->getTagsFree()) {
$article->addTagsFree($tagFreeEntity);
}
}
}
}
// on enregistre le pays
if ($article->getVille() != null) {
$article->setPays($article->getVille()->getPays());
} else if ($article->getVilleContribution() != null) {
$article->setPays($article->getVilleContribution()->getPays());
}
$em->persist($article);
$em->flush();
// flashbag en cas de succès
$this->addFlash('success', 'L\'enregistrement a été effectué avec succès !');
return $this->redirectToRoute('article_index_editorial');
}
return $this->render('TKSBlogBundle:Article:editEditorial.html.twig', array(
'article' => $article,
'edit_form' => $editForm->createView(),
));
}
我的观点:
{% extends 'TKSBlogBundle:Article:indexEditorial.html.twig' %}
{% block tab %}
<h3 class="title">Modifier l'article <a href="{{ path('article_index_editorial') }}" class="btn btn-gray-transparent btn-sm">Annuler</a></h3>
<hr>
{% form_theme edit_form '::baseform.html.twig' %}
{{ form_start(edit_form, { 'attr': { 'class': 'form-horizontal' } } ) }}
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.titre) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.titre) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.titre) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.legende) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.legende) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.legende) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
</div>
<div class="col-sm-6 col-xs-10 ">
<a class="btn btn-gray-transparent btn-animated collapsed" data-toggle="collapse" href="#collapseTexte" aria-expanded="false" aria-controls="collapseContent">Ajouter un texte <i class="fa fa-plus"></i></a>
</div>
</div>
<div class="collapse " id="collapseTexte" aria-expanded="false">
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.texte) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.texte) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.texte) }}</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
</div>
<div class="col-sm-6 col-xs-10 ">
<a class="btn btn-gray-transparent btn-animated collapsed" data-toggle="collapse" href="#collapseSource" aria-expanded="false" aria-controls="collapseContent">Ajouter une source <i class="fa fa-plus"></i></a>
</div>
</div>
<div class="collapse " id="collapseSource" aria-expanded="false">
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.source) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.source) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.source) }}</span>
</div>
</div>
</div>
<hr>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.lieu) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.lieu) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.lieu) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.adresse) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.adresse) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.adresse) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.ville) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.ville) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.ville) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
</div>
<div class="col-sm-6 col-xs-10 ">
La ville que vous cherchez n'apparaît pas dans la liste ?
<br>
<a class="btn btn-gray-transparent btn-animated collapsed" data-toggle="collapse" href="#collapseVille2" aria-expanded="false" aria-controls="collapseContent">Proposer une nouvelle ville <i class="fa fa-plus"></i></a>
</div>
</div>
<div class="collapse " id="collapseVille2" aria-expanded="false">
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.villeContribution) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.villeContribution) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.villeContribution) }}</span>
</div>
</div>
</div>
<hr>
{# TAGS #}
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.tags) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.tags) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.tags) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
</div>
<div class="col-sm-6 col-xs-10 ">
<a class="btn btn-gray-transparent btn-animated collapsed" data-toggle="collapse" href="#collapseTagsFree" aria-expanded="false" aria-controls="collapseContent">Ajouter un tag personnel <i class="fa fa-plus"></i></a>
</div>
</div>
<div class="collapse " id="collapseTagsFree" aria-expanded="false">
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.tagsFree) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.tagsFree) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.tagsFree) }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-10 control-label label-form">
{{ form_label(edit_form.tagsFreeNew) }}
</div>
<div class="col-sm-6 col-xs-10 ">
{{ form_widget(edit_form.tagsFreeNew) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-xs-1"></div>
<div class="col-sm-6 col-xs-9">
<span class="text-danger">{{ form_errors(edit_form.tagsFreeNew) }}</span>
</div>
</div>
</div>
<div class="label-form text-center">
<button type="submit" id="form_submit" name="form[submit]" class="btn btn-sm btn-default">Enregistrer et continuer</button>
</div>
{{ form_end(edit_form) }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<!-- javascript pour formulaires imbriqués Tags -->
<script type="text/javascript">
jQuery(document).ready(function () {
$("input[type='checkbox']").before("<br>");
$("div#article_tags").addClass("checkbox");
});
</script>
{% endblock %}
任何可以帮助我的想法?