我正在使用symfony 2,我有一个表格,我在其上放置@Assert\NotBlank()注释.我正在填充字段,我的表单没有通过isValid和isSubmitted测试,在这些行之后我得到一个非null值的退出(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)不会呈现错误.为什么?我为什么要进一步了解问题的来源?
我有一个非常复杂的类型.对于我网站的其他一些形式,错误被正确显示.我的风格:
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) }}
这使得标签,表单元素和错误都在同一个镜头.