質問
Doctrine検証のデフォルトエラーメッセージを変更する必要があります。どうやって これはできますか?
ありがとう
解決
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のおかげで、Modelクラスでそのメソッドをオーバーロードできます...
しかし、少しきれいにするために、私はそうします:
-
Doctrine_Record
を拡張する新しいクラスを作成します-たとえば - そのクラスはそのメソッドを再定義して、エラーメッセージのカスタマイズを許可します
- Modelクラスは、その
My_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が提供するメソッドに触発されていることに気付くでしょう-これは正常なようです、^^
注意すべきもう1つの点:
- _
getValidationFailed
メソッドを定義して呼び出します - エラーメッセージを作成する必要があります。デフォルトの動作を使用する場合は
null
を返します - また、モデルクラスの
_getValidationFailed
メソッドをオーバーロードして、;-) をカスタマイズできます。
そして今、私のModelクラス:
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
を拡張し、モデルの name
フィールドの検証エラーを処理する _getValidationFailed
メソッドを定義します。
さて、レコードをロードするためにそれを行うとしましょう:
$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)
&quot; name
&quot;のメッセージが表示されます。カスタマイズされており、&quot; value
&quot;デフォルトのDoctrineのものから来ています。
結論として、簡単ではありませんが、実行可能な;-)
そして今、あなたの問題の正確な解決へのガイドとしてこれを使用するのはあなた次第です:-)
さらにコーディングが必要になると思います...しかし、あなたは本物からそれほど遠くない!
楽しんでください!
他のヒント
現在のバージョンでは不可能です!!!