教義検証のデフォルトのエラーメッセージを変更する必要があります。これどうやってするの?
2 に答える
ある意味、 CrazyJoeは正しいです。ハードワークなしでは不可能です:-(
でも、根気よく探せば、方法が見つかるかもしれません;-)
Doctrine 1.1 では、モデル クラスを extends し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 クラスでそのメソッドをオーバーロードできます...
しかし、もう少しきれいにするために、私は次のようにします:
- 新しいクラスを作成します-たとえば
My_Doctrine_Record
、それを拡張しますDoctrine_Record
- そのクラスはそのメソッドを再定義して、エラーメッセージのカスタマイズを可能にします
- Model クラスはそのクラスを拡張し
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;
}
}
このメソッドは、Doctrine が提供するメソッドに触発されていることに気付くでしょうgetErrorStackAsString
-- これは普通のことのように思えます ^^
注目すべきもう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
するメソッドを定義します。_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
が正しくありません...そのため、検証メソッドを使用して、次のエラー メッセージを生成します。
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 Thing からのものです。
結論として、簡単ではありませんが、実行可能です;-)
そして今、これをあなたの問題の正確な解決策へのガイドとして使用するのはあなた次第です:-)
もう少しコーディングが必要になると思います...しかし、あなたは本当の取引から遠く離れていません!
楽しむ !
現在のバージョンでは不可能です!!!