Symfony php模板中自定义验证器的翻译
我为自己构建了一个自定义约束验证器。验证器正在工作。但是如何在php模板中翻译自定义验证器的错误消息呢?其他验证程序消息正在工作,因此我在Symfony php模板中自定义验证器的翻译,symfony,translation,Symfony,Translation,我为自己构建了一个自定义约束验证器。验证器正在工作。但是如何在php模板中翻译自定义验证器的错误消息呢?其他验证程序消息正在工作,因此我在app/config/validators.XX.yml中有翻译 在我的行动中: $form = $this->createFormBuilder() ->add('date_id', 'choice', array( .... 'constraints' =
app/config/validators.XX.yml
中有翻译
在我的行动中:
$form = $this->createFormBuilder()
->add('date_id', 'choice', array(
....
'constraints' => array(new CheckChoicesDateId(array('date_ids' => $date_ids))),
....
))
捆绑包/验证程序/约束中
class CheckChoicesDateId extends Constraint
{
public $invalidMessage = '{{ value }}';
public $date_ids;
public function __construct($options = null)
{
parent::__construct($options);
if (null === $this->date_ids ) {
throw new MissingOptionsException('Option date_ids must be given for constraint ' . __CLASS__, array('date_ids'));
}
}
}
class CheckChoicesDateIdValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if ($value == NULL || !isset($value)) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => 'error.date.0',
//I also tried $this->get('translator')->trans('error.date.0');
// with the error message: Call to undefined method GET
));
}
if (is_numeric($value)) {
$t = array_key_exists($value, $constraint->date_ids);
if ($t == NULL) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => 'error.date.1',
));
}
}
return;
}
}
捆绑包/验证程序/约束中
class CheckChoicesDateId extends Constraint
{
public $invalidMessage = '{{ value }}';
public $date_ids;
public function __construct($options = null)
{
parent::__construct($options);
if (null === $this->date_ids ) {
throw new MissingOptionsException('Option date_ids must be given for constraint ' . __CLASS__, array('date_ids'));
}
}
}
class CheckChoicesDateIdValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if ($value == NULL || !isset($value)) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => 'error.date.0',
//I also tried $this->get('translator')->trans('error.date.0');
// with the error message: Call to undefined method GET
));
}
if (is_numeric($value)) {
$t = array_key_exists($value, $constraint->date_ids);
if ($t == NULL) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => 'error.date.1',
));
}
}
return;
}
}
在我的模板中:
<?php echo $view['form']->errors($form['date_id']) ?>
//I also tried
<?php echo $this->get('translator')->trans($view['form']->errors($form['date_id'])) ?>
//我也试过了
我确实有一个解决方案,但我想这并不好:
在操作中,我会为每个可能的错误传递一个变量
'constraints' => array(new CheckChoicesDateId(array('date_ids' => $date_ids, 'error_date_0 => $this->get('translator')...., 'error_date_1 => $this->get('translator').... ))),
在自定义验证器中,我使用$constraint->error\u date\u X
为每个错误调用正确的变量
不太好,但它正在工作。如果有人有更好的解决方案,请随时发布 把你的
validators.XX.yml
放在app/Resources/{your_bundle}/translations/
或src/your/bundle/Resources/transalition/
中。但这没用。有什么建议吗?嘿,如果你有兴趣,我发布了一个解决方案。无论如何,谢谢你的建议。