3

教義検証のデフォルトのエラーメッセージを変更する必要があります。これどうやってするの?

4

2 に答える 2

6

ある意味、 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するメソッドを定義します。_getValidationFailedname


さて、レコードをロードするためにそれを行うとしましょう:

$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 からのものです。


結論として、簡単ではありませんが、実行可能です;-)

そして今、これをあなたの問題の正確な解決策へのガイドとして使用するのはあなた次第です:-)
もう少しコーディングが必要になると思います...しかし、あなたは本当の取引から遠く離れていません!

楽しむ !

于 2009-08-05T18:56:37.617 に答える
0

現在のバージョンでは不可能です!!!

于 2009-08-05T16:46:31.810 に答える