没有显示Symfony3表单错误(只是notNull / notBlank),其余的都在工作

时间:2016-05-25 09:50:27

标签: php forms validation symfony notnull

我尝试验证symfony3表单,我为每个实体字段使用两个约束(NotBlank和Float)。浮动约束的错误消息正确显示,因为NotBlank错误消息仅针对整个表单(全局错误)显示,而不是针对每个分离的字段。我也尝试使用NotNull而不是NotBlank,这对我没用。

Bellow我已经复制/粘贴了一段代码。

class ParametersType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('variable_sheer_header',  NumberType::class, [
                'label' => 'parameters.variable_sheer_header',
                'error_bubbling' => true ,
                'attr' => [
                    'placeholder' => 'parameters.variable_sheer_header',
                    "class" => 'form-control'
                ],
            ])
            ->add('variable_sheer_hem',     NumberType::class, [
                'label' => 'parameters.variable_sheer_hem',
                'error_bubbling' => true ,
                'attr' => [
                    'placeholder' => 'parameters.variable_sheer_hem',
                    "class" => 'form-control'
                ],
            ])
            ->add('variable_sheer_overlap', NumberType::class, [
                'label' => 'parameters.variable_sheer_overlap',
                'error_bubbling' => true ,
                'attr' => [
                    'placeholder' => 'parameters.variable_sheer_overlap',
                    "class" => 'form-control'
                ],
            ])
    ...

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @MongoDB\EmbeddedDocument()
 */
class ParametersEmbedded
{
    /**
     * @var float
     *
     * @Assert\Type(type="float", message="parameters.field.not.float")
     * @Assert\NotBlank(message="parameters.field.not.empty")
     *
     * @MongoDB\Field(type="float")
     */
    protected $variableSheerOverlap;

    /**
     * @var float
     *
     * @Assert\NotNull(message="parameters.field.not.empty")
     * @Assert\Type(type="float", message="parameters.field.not.float")
     *
     * @MongoDB\Field(type="float")
     */
    protected $variableSheerRod;

-#index.html.haml



= form_start(form)
.settings-form
  %p.title-form= 'user.parameters'|trans
  .row
    .col-xs-4
      %h4.parameters-group-name='parameters.variable_sheer'|trans
      = form_row(form.variable_sheer_hem)
      = form_errors(form.variable_sheer_hem)
      = form_row(form.variable_sheer_overlap)
      = form_errors(form.variable_sheer_overlap)
      = form_row(form.variable_sheer_rod)
      = form_errors(form.variable_sheer_rod)
      = form_row(form.variable_sheer_header)
      = form_errors(form.variable_sheer_header)
    .col-xs-4
      %h4.parameters-group-name='parameters.other'|trans
      = form_row(form.other_hem)
      = form_errors(form.other_hem)
      = form_row(form.other_overlap)
      = form_errors(form.other_overlap)
      = form_row(form.other_rod)
      = form_errors(form.other_rod)
      = form_row(form.other_header)
      = form_errors(form.other_header)
    .col-xs-4
      %h4.parameters-group-name='parameters.shade'|trans
      = form_row(form.shade_add_hem_length)
      = form_errors(form.shade_add_hem_length)
      = form_row(form.shade_add_hem_width)
      = form_errors(form.shade_add_hem_width)
= form_row(form.submit)ubmit)
= form_end(form)

// Controller


public function profileParametersAction(Request $request)
{
     $form = $this->createForm(ParametersType::class);
     $form->handleRequest($request);
  
     if ($form->isValid()) {
         $this->getUser()->setParameters($form->getData());
         $this->getDM()->flush();
         $this->addFlash('success', 'user.profile.parameters.changed');
     }

     return $this->render('AppBundle:Profile:parameters.html.haml', [
       'form' => $form->createView()
     ]);
}

1 个答案:

答案 0 :(得分:0)

您希望在表单中使用required as true属性。

点击此处:http://symfony.com/doc/current/reference/forms/types/form.html#required