我为check值创建了自定义断言,只有数字,我需要为这个服装断言创建翻译消息。我创造了所有需要的东西,但在验证之后我得到了反式密钥而不是反式消息
[
{
"property_path": "phone",
"message": "contains.numeric"
}
]
这就是我所拥有的
\\AppBundle\Validator\Constraints\ContainsNumericValidator
class ContainsNumericValidator extends ConstraintValidator
{
private $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^[0-9]+$/', $value, $matches)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
\\AppBundle\Validator\Constraints\ContainsNumeric
/**
* @Annotation
*/
class ContainsNumeric extends Constraint
{
public $message = 'contains.numeric';
}
并在我的实体中
use AppBundle\Validator\Constraints as AppAssert;
/**
* @ORM\Table(name="users")
*/
class User extends AbstractUser implements UserInterface
{
/**
* @AppAssert\ContainsNumeric()
* @ORM\Column(name="phone", type="string", nullable=true)
*/
private $phone;
和我的xlf app/Resources/translations/validators.en.xlf
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="contains.numeric">
<source>contains.numeric</source>
<target>Please enter an valid data.</target>
</trans-unit>
</body>
</file>
</xliff>
答案 0 :(得分:0)
在您的翻译文件中,您需要将resname
属性设置为:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="contains.numeric" resname="contains.numeric">
<source>contains.numeric</source>
<target>Please enter an valid data.</target>
</trans-unit>
</body>
</file>
</xliff>
实际上,resname
值是用于选择翻译的关键。