jjzjj

php - symfony2 : form errors not displayed in twig despite a non null getErrorsAsString()

coder 2024-01-04 原文

我正在使用 symfony 2,我有一个表单,我在上面放置了 @Assert\NotBlank() 注释。 我自愿填写字段,我的表单没有通过 isValid 和 isSubmitted 测试,在这些行之后我得到一个非空值 exit(var_dump($recipeForm->getErrors()));

  private 'errors' => 
    array (size=4)
      0 => 
        object(Symfony\Component\Form\FormError)[4119]
          private 'message' => string 'Cette valeur doit être vide.' (length=29)
          protected 'messageTemplate' => string 'This value should be blank.' (length=27)
          protected 'messageParameters' => 
            array (size=1)
              ...
          protected 'messagePluralization' => null
          private 'cause' => 
            object(Symfony\Component\Validator\ConstraintViolation)[4062]
              ...
          private 'origin' => null

在我的 Twig 模板中,我用一个简单的表单(表单)呈现表单。 form_errors(form) 或 form_errors(form.field) 不会呈现错误。为什么?我为什么要进一步了解问题出在哪里?

我的类型很复杂。对于我网站的其他一些形式,错误显示正确。 我的类型:

<?php
//src/AppBundle/Form/FoodAnalytics/RecipeType.php
namespace AppBundle\Form\FoodAnalytics;

use AppBundle\Form\Core\MediaType;
use AppBundle\Repository\FoodAnalytics\UnitRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RecipeType extends AbstractType
{
    protected $recipeIngredientQueryBuilder;
    protected $recipeSubrecipeQueryBuilder;
    protected $unitRepository;
    protected $action;

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_foodanalytics_recipe' . $this->action;
    }

    public function __construct(UnitRepository $unitRepository, $recipeIngredientQueryBuilder=null, $recipeSubrecipeQueryBuilder=null, $action = null)
    {
        $this->recipeIngredientQueryBuilder = $recipeIngredientQueryBuilder;
        $this->recipeSubrecipeQueryBuilder = $recipeSubrecipeQueryBuilder;
        $this->unitRepository = $unitRepository;
        $this->action = $action == null ? null : '_' . $action;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('recipeCategories', 'genemu_jqueryselect2_entity',array(
                    'multiple' => true,
                    'class' => 'AppBundle:FoodAnalytics\RecipeCategory',
                    'label' => 'Catégories',
                    'required' => false,
                    'by_reference' => false,
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Indiquez les catégories dans lesquelles enregistrer la recette pour un recherche future plus facile",
                    )))
            ->add('isProduct', null, array(
                    'label'=>'Correspond à un produit fini',
                    'required'=>false,
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"La recette correspond-elle à un produit fini qui peut être mis en vente ?",
                    )))
            ->add('name', null, array(
                    'label'=>'Nom détaillé',
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Indiquez le nom détaillé de la recette. Par exemple : 'millefeuilles praliné ganache vanille sur feuilletage inversé'",
                    )))
            ->add('nickName', null, array(
                    'label'=>'Nom raccourci',
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Indiquez un nom raccourci pour la recette. Par exemple : 'millefeuilles'",
                    )))
            ->add('recipeIngredients', 'collection', array(
                    'type' => new RecipeIngredientType($this->unitRepository, $this->recipeIngredientQueryBuilder),
                    'by_reference' => false,
                    'label'=>'Ingrédient',
                    'allow_add' => true,
                    'allow_delete' => true,
                    'cascade_validation' => true,
                ))
            ->add('subrecipes', 'collection', array(
                    'type' => new RecipeSubrecipeType($this->unitRepository, $this->recipeSubrecipeQueryBuilder),
                    'by_reference' => false,
                    'label'=>'Sous-recette',
                    'allow_add' => true,
                    'allow_delete' => true
                ))
            ->add('recipeSteps', 'collection', array(
                    'type' => new RecipeStepType(),
                    'by_reference' => false,
                    'label'=>'Etape de production',
                    'allow_add' => true,
                    'allow_delete' => true
                ))
            ->add('portions', null, array(
                    'label'=>'Nombre de parts / de pièces',
                    'required' => false,
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Indiquez le nombre d'éléments disponibles dans la recette. Cela peut permettre d'utiliser l'unité 'U' dans les recettes parentes qui l'utiliseront",
                    )))
            ->add('shortDescription', null, array(
                    'label'=>'Description courte',
                    'required'=>false,
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Décrivez succinctement la recette",
                    )))
            ->add('medias', 'collection', array(
                    'type' => new MediaType(),
                    'by_reference' => false,
                    'label'=>'Medias',
                    'allow_add' => true,
                    'allow_delete' => true,
                    'required' => false,
                    'attr'=>array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'title'=>"Ajoutez des images ou vidéos pour décrire la recette",
                    )))
            ->add('content', 'textarea', array(
                    'label'=>'Instructions générales',
                    'required'=>false,
                    'attr' => array(
                        'data-toggle'=>"tooltip",
                        'data-placement'=>"top",
                        'class' => 'summernote',
                        'title'=>"Ajoutez du contenu supplémentaire pour détailler la recette",
                    )))
            ->add('workingDuration', 'timepicker', array(
                        'label'=>'Temps total de travail',
                        'required' => false,
                        'attr'=>array
                        (
                            'class' => 'timepicker',
                            'data-toggle'=>"tooltip",
                            'data-placement'=>"top",
                            'title'=>"Indiquez le temps total de travail consacré à la recette si il diffère du temps de travail cumulé des étapes de production",
                        )))
            ->add('sleepDuration', 'timepicker', array(
                    'label'=>'Temps total de repos',
                    'required' => false,
                    'attr'=>array
                    (
                        'data-toggle'=>"tooltip",
                        'class'=>'timepicker',
                        'data-placement'=>"top",
                        'title'=>"Indiquez le temps total de repos consacré à la recette si il diffère du temps de repos cumulé des étapes de production",
                    )))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\FoodAnalytics\Recipe',
//            'cascade_validation' => true,
        ));
    }
}

编辑:只保留此表单的一个简单字段不会改变任何内容,错误不会显示

最佳答案

看看这里是如何呈现错误的:http://symfony.com/doc/current/cookbook/form/form_customization.html#customizing-error-output

使用 {{ form_errors(form) }} 的问题是它显示全局表单错误,而不是单个字段,而 getErrorsAsString() 将向下钻取所有领域。如果您希望通过 {{ form_errors(form) }} 访问所有单个字段错误,则需要编辑表单中的每个字段并添加选项 error_bubbling =>是的

如果您没有将我们所有字段的错误冒泡设置为 true,那么您将需要单独呈现每个字段的错误 - 例如:{{ form_errors(form.name) }},或者只使用 {{ form_row(form.name) }} 一次渲染标签、表单元素和错误。

关于php - symfony2 : form errors not displayed in twig despite a non null getErrorsAsString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27619838/

有关php - symfony2 : form errors not displayed in twig despite a non null getErrorsAsString()的更多相关文章

随机推荐