【问题标题】:Translating Custom Validator messages with parameters使用参数翻译自定义验证器消息
【发布时间】:2014-04-02 12:36:57
【问题描述】:

我有带有定义的 symfony2 自定义验证器:

// Nip.php

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class Nip extends Constraint
{
    public $message = 'This value %string% is not a valid NIP number';
//...

这是验证码:

// NipValidator.php

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class NipValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {

        $stringValue = (string) $value;

        $nip = preg_replace('/[ -]/im', '', $stringValue);
        $length = strlen($nip);

        if ($length != 10) {
            $this->context->addViolation(
                $constraint->message,
                array('%string%' => $value)
            );

            return;
        }
//...

还有我的翻译文件:

// validators.pl.yml

validator.nip: %string% to nie jest poprawny nip

服务定义:

// services.yml

services:
    validator.nip:
        class: BundlePath\Validator\Constraints\NipValidator
        tags:
            - { name: validator.constraint_validator, alias: validator.nip }

我尝试将 $constraint->message 替换为“validator.nip”,但这只会将“validator.nip”显示为字符串,并且无法解析为已翻译的消息。

CustomValidator 效果很好,唯一的问题是启用翻译。 我从 symfony.com 阅读了有关约束翻译的文档,但这是针对标准验证器而不是自定义验证器的。

【问题讨论】:

    标签: validation symfony translation


    【解决方案1】:

    我认为问题在于您如何定义消息密钥和构建验证翻译域。

    您的自定义 nip 约束消息作为翻译键会更好:

    $message = error.nip.invalidNumber;
    

    翻译文件应为:

    error:
        nip:
            invalidNumber: %string% to nie jest poprawny nip
    

    您已定义与实际服务名称 (validator.nip) 相同的服务别名。我不确定这是否会导致错误,但为了安全起见,我会将别名设置为 app_nip_validator。

    最后,在您的自定义验证器中,删除返回并更改您构建违规的方式。

            if ($length != 10) {
                $this->context->buildViolation($constraint->message)
                    ->setParameter('%string%', $value)
                    ->addViolation();
    
                // return;
            }
    

    【讨论】:

    • 使用自定义翻译文件时,可以使用:$this->context->buildViolation($constraint->message) ->setParameter('%string%', $value)->setTranslationDomain('mydomain') ->addViolation();
    【解决方案2】:
    $this->context->addViolationAt('your_path', 'translation.messate', $parameters);
    

    【讨论】:

      【解决方案3】:

      您可以在自定义验证器中注入翻译器。

      // services.yml 
         services:
              validator.nip:
                  class: BundlePath\Validator\Constraints\NipValidator
                  arguments: [ "@translator" ]
                  tags:
                      - { name: validator.constraint_validator, alias: validator.nip }
      

      然后用它来准备消息:

      // NipValidator.php
          protected $translator;
      
          public function __construct($translator)
          {
              $this->translator = $translator;
          }
      
          // ....
              $this->context->addViolation(
                      $this->translator->trans($constraint->message, array(
                          '%string%' => $value,
                      ))
              );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-22
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        相关资源
        最近更新 更多