Symfony2: form errors not showing in branch despite null getErrorsAsString () missing

I am using symfony 2, and I have a form on which I put @Assert \ NotBlank () annotations. I voluntarily fill in the fields, my form does not pass the test isValid and isSubmitted, and after these lines I get a non-zero value to 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 

In my branch template, I am making a form with a simple form (form). form_errors (form) or form_errors (form.field) will not display errors. What for? Why can I do to better understand where the question came from?

I have a pretty complicated type. for some other forms of my website, errors display correctly. My type:

 <?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, )); } } 

EDIT: saving only one simple field of this form does not change anything, an error will not be displayed

+5
source share
3 answers

See how the errors are displayed here: http://symfony.com/doc/current/cookbook/form/form_customization.html#customizing-error-output

The problem with using {{ form_errors(form) }} is that it displays global form errors, not individual fields, whereas getErrorsAsString() will go through all the fields. If you want all individual field errors to be accessible through {{ form_errors(form) }} , you need to edit each individual field in your form and add the option error_bubbling => true .

If you do not set the popup error to true in all of our fields, you will need to visualize each field error separately - for example: {{ form_errors(form.name) }} or simply using {{ form_row(form.name) }} , which displays label, shape element and errors in just one shot.

+6
source

Oh my god!

Sorry, I made people lose time with this! The problem was that I was passing $response = array(form->createView()) as the response, but this array response was generated before I processed the request. Therefore, I could see it in a wardome, and not in a branch.

+3
source

It seems that the error is caused by a property validation rule that is part of your Recipe object but not added to RecipeType , so the error cannot be associated with any subforms.

Try to check your object and find which property has a validation rule and is not added to RecipeType either add a default value for this property, or use Validation Groups

0
source

Source: https://habr.com/ru/post/1209766/


All Articles