如何“有效地”设置 sfValidatorInteger 等的默认错误消息?
我刚刚遇到了一个问题,我认为这是 symfony 框架的某种“错误”:我想为不同类型的验证器(例如 sfValidatorInteger)设置默认错误消息。为此,在我的 ProjectConfiguration::setup
方法中,我有:
sfValidatorBase::setDefaultMessage('required', 'Erforderlich.');
sfValidatorBase::setDefaultMessage('invalid', 'Format ungültig.');
sfValidatorInteger::setDefaultMessage('invalid', '"%value%" ist keine Ganzzahl.');
这对于 sfValidatorBase
工作正常,但不适用于例如 sfValidatorInteger
。 sfValidatorInteger
仍然显示预设的默认消息 '"%value% 不是整数。'
。
因此,我查看了 sfValidatorInteger::configure 的实现
并发现它将实例的错误消息设置为硬编码:
protected function configure($options = array(), $messages = array())
{
$this->addMessage('max', '"%value%" must be at most %max%.');
$this->addMessage('min', '"%value%" must be at least %min%.');
$this->addOption('min');
$this->addOption('max');
$this->setMessage('invalid', '"%value%" is not an integer.');
}
我本来期望错误消息是从默认消息映射中读取的,但显然,它们不是,
那么,我该如何解决这个问题 。以干净的方式最好不触及原始的 symfony 代码(因为我的更改可能会在将来的更新期间被覆盖)?我应该派生所有验证器类以提供适当的 configure
实现吗?这么认为。为每个验证器实例单独设置错误消息也不是(干净的)选项。
也许我在验证器的配置方面遇到了问题。然后,一个更普遍的问题是:如何为不同的验证器设置默认错误消息。 sfValidatorInteger
的验证器类如 'invalid' => '"%value%" ist keine Ganzzahl.'
?
预先感谢,
弗林施。
I just rumbled into a problem that I assume to be some kind of 'bug' of the symfony framework: I want to set default error messages for different types of validators (e.g. sfValidatorInteger
). For this, inside of my ProjectConfiguration::setup
method, I have:
sfValidatorBase::setDefaultMessage('required', 'Erforderlich.');
sfValidatorBase::setDefaultMessage('invalid', 'Format ungültig.');
sfValidatorInteger::setDefaultMessage('invalid', '"%value%" ist keine Ganzzahl.');
This works fine for sfValidatorBase
but not for, for example, sfValidatorInteger
. sfValidatorInteger
still shows the preset default message '"%value% is not an integer.'
.
So, I had a look at the implementation of sfValidatorInteger::configure
and figured out that it sets the instance's error messages hard-coded:
protected function configure($options = array(), $messages = array())
{
$this->addMessage('max', '"%value%" must be at most %max%.');
$this->addMessage('min', '"%value%" must be at least %min%.');
$this->addOption('min');
$this->addOption('max');
$this->setMessage('invalid', '"%value%" is not an integer.');
}
I'd have expected that the error messages are read from the default message map but, obviously, they are not.
So, how could I solve this problem in a clean way preferably without touching the original symfony code (as my changes could be overwritten during future updates)? Should I derive all the validator classes to provide an appropriate configure
implementation? Well, I don't think so. Setting the error messages for each validator instance separately would also be no (clean) option.
Maybe I got something wrong concerning the configuration of them validators. Then, a more general question would be: How to set default error messages for different validator classes like 'invalid' => '"%value%" ist keine Ganzzahl.'
for sfValidatorInteger
?
Thanks in advance,
Flinsch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建自己的验证器来扩展默认的 Symfony 验证器并使用它们。
I would create my own validators extending the default Symfony ones and use those.
我认为您可以使用 xliff 翻译文件来做到这一点。
I think you could do this using an xliff translation file.