在“ProjectConfiguration”中,我可以自定义输出错误,例如
sfValidatorBase::setDefaultMessage('min_length', 'Min% min_length% sign');
sfValidatorBase::setDefaultMessage('required', 'Label required');
但对于“必需”字段,我无法替换动态字段名称,如下所示:
sfValidatorBase::setDefaultMessage('required', 'Label %label% required');
我该如何解决?
答案 0 :(得分:0)
你不能。或者你将不得不入侵全球sfValidatorBase
。
如果您以min_length
错误为例,它会像这样抛出(在sfValidatorString
中):
throw new sfValidatorError($this, 'min_length', array('value' => $value, 'min_length' => $this->getOption('min_length')));
正如您所看到的,它需要value
和min_length
的参数,因为消息的定义如下:
$this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).');
因此它会自动将每个变量替换为%....%
。
对于required
错误,它从sfValidatorBase
(由所有验证程序扩展的那个)抛出:
throw new sfValidatorError($this, 'required');
如您所见,没有任何内容作为第三个参数,因此您无法配置额外参数。如果你想这样做,你应该改变:
throw new sfValidatorError($this, 'required', array('label' => $this->getOption('label')));
但是这个更改将在Symfony存储库中。此sfValidatorBase
扩展到每个验证器,我不知道如何应用此修改。至少,你知道在哪里做出改变。