How to "effectively" set default error messages of sfValidatorInteger etc.?
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.');
sfVa开发者_如何学JAVAlidatorInteger::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.I would create my own validators extending the default Symfony ones and use those.
I think you could do this using an xliff translation file.
<source>"%value%" is not an integer.</source>
<target>"%value%" n'est pas un entier.</target>
精彩评论