代码之家  ›  专栏  ›  技术社区  ›  Roubi

Symfony3:如何翻译eventListeners中添加的FormErrors?

  •  0
  • Roubi  · 技术社区  · 7 年前

    //...
    class BananaType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('flavor');
            $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
                $form = $event->getForm();
                if ($form->get('flavor')->getData() === null) {
                    $form->addError(new FormError('form.error.flavor_missing'));
                }
            }
        }
    }
    

    即使信息 缺少form.error.flavor\u

    你知道怎么翻译吗?我希望我不必注射疫苗 translator 使用这种代码以各种形式提供服务以解决此问题。

    2 回复  |  直到 7 年前
        1
  •  0
  •   acucchieri    7 年前

    为什么不使用 Validation Constraints

    use Symfony\Component\Validator\Constraints as Assert;
    
    // ...
    
    class BananaType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('flavor', YourType::class, array(
                'constraints' => array(
                    new Assert\NotNull(), // or any other
                ),
            ));
        }
    }
    

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('flavor', YourType::class, array(
                'constraints' => array(
                    new Assert\NotNull(array(
                        'message' => 'form.error.flavor_missing'
                    )),
                ),
            ));
        }
    
        2
  •  0
  •   Oscar Manuel Gómez Senovilla    7 年前

    如果打开开发环境,应该会看到缺少的字符串。看看它们,您应该会看到域和预期的消息文件。

    推荐文章