我需要修改教义验证的默认错误消息。我怎样才能做到这一点?
2 回答
CrazyJoe是对的,在某种程度上:没有一些努力是不可能的:-(
但是,如果你足够努力地搜索,你可能会找到一种方法;-)
使用 Doctrine 1.1,您的模型类扩展Doctrine_Record
.
该类定义了这个方法:
/**
* Get the record error stack as a human readable string.
* Useful for outputting errors to user via web browser
*
* @return string $message
*/
public function getErrorStackAsString()
{
$errorStack = $this->getErrorStack();
if (count($errorStack)) {
$message = sprintf("Validation failed in class %s\n\n", get_class($this));
$message .= " " . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " had validation error" . (count($errorStack) > 1 ? 's' : null) . ":\n\n";
foreach ($errorStack as $field => $errors) {
$message .= " * " . count($errors) . " validator" . (count($errors) > 1 ? 's' : null) . " failed on $field (" . implode(", ", $errors) . ")\n";
}
return $message;
} else {
return false;
}
}
这是生成消息的方法;如您所见,它是全自动的,根本无法配置:-(
尽管如此,由于 OOP,我们可以在模型类中重载该方法......
但是,为了更清洁,我会:
- 创建一个新类——比如说
My_Doctrine_Record
,扩展Doctrine_Record
- 该类将重新定义该方法,以允许自定义错误消息
- 我们的模型类将扩展
My_Doctrine_Record
该类。
这将避免在我们的每个模型类中重复该方法;并可能在另一天证明有用......
当然,我们的My_Doctrine_Record::getErrorStackAsString
方法可以依赖模型类的方法来帮助生成消息,并为每个模型类进行特殊定制。
这是一个工作示例;远非完美,但它可能会引导您获得想要的东西;-)
首先,初始化:
require_once '/usr/share/php/Doctrine/lib/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$conn = Doctrine_Manager::connection('mysql://test:123456@localhost/test1');
我猜你的应用程序中已经有类似的东西......
接下来,我们的新My_Doctrine_Record
课程:
class My_Doctrine_Record extends Doctrine_Record
{
public function getErrorStackAsString()
{
$errorStack = $this->getErrorStack();
if (count($errorStack)) {
$message = sprintf("BAD DATA in class %s :\n", get_class($this));
foreach ($errorStack as $field => $errors) {
$messageForField = $this->_getValidationFailed($field, $errors);
if ($messageForField === null) {
// No custom message for this case => we use the default one.
$message .= " * " . count($errors) . " validator" . (count($errors) > 1 ? 's' : null) . " failed on $field (" . implode(", ", $errors) . ")\n";
} else {
$message .= " * " . $messageForField;
}
}
return $message;
} else {
return false;
}
}
protected function _getValidationFailed($field, $errors) {
return null;
}
}
你会注意到这个getErrorStackAsString
方法的灵感来自于 Doctrine 提供的方法——这看起来很正常,我会说 ^^
另一件需要注意的事情:
- 它定义并调用 _
getValidationFailed
方法 - 应该创建错误消息;
null
如果我们想使用默认行为,或者返回 - 我们可以在我们的模型类中重载该
_getValidationFailed
方法,以自定义东西;-)
现在,我的模型类:
class Test extends My_Doctrine_Record
{
protected function _getValidationFailed($field, $errors) {
switch ($field) {
case 'name':
return "You entered wrong data from 'name' field.\n Errors are for '"
. implode("', '", $errors) . "'\n";
break;
// other fields ?
default:
return null;
}
}
public function setTableDefinition()
{
$this->setTableName('test');
$this->hasColumn('id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => 0,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('name', 'string', 32, array(
'type' => 'string',
'length' => 32,
'fixed' => false,
'notnull' => true,
'email' => true,
));
$this->hasColumn('value', 'string', 128, array(
'type' => 'string',
'length' => 128,
'fixed' => false,
'notnull' => true,
'htmlcolor' => true,
));
$this->hasColumn('date_field', 'integer', 4, array(
'type' => 'timestamp',
'notnull' => true,
));
}
}
它扩展My_Doctrine_Record
并定义了一种_getValidationFailed
方法,用于处理name
我的模型字段上的验证错误。
现在,假设我这样做是为了加载一条记录:
$test = Doctrine::getTable('Test')->find(1);
var_dump($test->toArray());
让我们尝试修改它,设置“坏”值:
$test->name = (string)time();
$test->value = 'glop';
try {
$test->save();
} catch (Doctrine_Validator_Exception $e) {
echo '<pre>';
echo $e->getMessage();
echo '</pre>';
die;
}
name
和字段都不value
OK...所以,我们将通过我们的验证方法,并生成此错误消息:
BAD DATA in class Test :
* You entered wrong data from 'name' field.
Errors are for 'email'
* 1 validator failed on value (htmlcolor)
您可以看到“ name
”的消息已被自定义, “”的消息value
来自默认的 Doctrine 事物。
所以,总结一下:不容易,但可行;-)
而且,现在,您可以将其用作问题的确切解决方案的指南:-)
我认为它需要更多的编码......但您离真正的交易不远了!
玩得开心 !
在当前版本中是不可能的!!!