1
커스텀 제약 검사기를 만들었습니다. 유효성 검사기가 작동 중입니다. 하지만 PHP 템플릿의 CUSTOM 유효성 검사기의 오류 메시지를 어떻게 번역 할 수 있습니까? 다른 유효성 검사기 메시지가 작동하므로 app/config/validators.XX.yml
에 번역이 있습니다. 내 행동에PHP 템플릿의 사용자 정의 유효성 검사기 번역
: 번들/검사기에서
$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;
}
}
:
<?php echo $view['form']->errors($form['date_id']) ?>
//I also tried
<?php echo $this->get('translator')->trans($view['form']->errors($form['date_id'])) ?>
'validator.XX.yml'을'app/Resources/{your_bundle}/transalations /'또는'src/your/bundle/Resources/transalition /'에 넣으십시오. –
나는했다. 그러나 도움이되지 않습니다. 어떤 제안? – craphunter
이봐, 관심이 있다면 해결책을 게시했다. 어쨌든 조언을 주셔서 감사합니다. – craphunter