关于理论验证的自定义错误消息

问题描述 投票:3回答:2

我需要修改理论验证的默认错误消息。怎么样我可以这样做吗?

php doctrine
2个回答
6
投票

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类中重载该方法...

但是,为了更干净一点,我会:

[创建一个新类-例如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; } }

    [您会注意到getErrorStackAsString方法是受教义所提供的方法启发的-这似乎很正常,我想说^^

    另一件事要注意:

    它定义并调用_ getValidationFailed方法

      那个应该创建错误消息;或返回null(如果我们要使用默认行为)>
    • 并且我们可以在Model类中重载_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;
    }
    

    namevalue字段都不正确...因此,我们将遍历验证方法,并生成此错误消息:

    BAD DATA in class Test :
        * You entered wrong data from 'name' field.
          Errors are for 'email'
        * 1 validator failed on value (htmlcolor)
    

    您可以看到“ name”的消息已被定制,而“ value”的消息来自默认的教义。

    因此,总结一下:不容易,但可行

    ;-)

    并且,现在,由您决定使用它作为解决问题的精确方法的指南:-)

    我认为,这将需要更多的编码。但是您离真正的交易还很遥远!

    玩得开心!


  • 0
    投票
    © www.soinside.com 2019 - 2024. All rights reserved.