Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条令验证上的自定义错误消息_Php_Doctrine - Fatal编程技术网

Php 条令验证上的自定义错误消息

Php 条令验证上的自定义错误消息,php,doctrine,Php,Doctrine,我需要修改条令验证的默认错误消息。怎么 我可以这样做吗?在当前版本中不可能 在某种程度上是正确的:没有一些艰苦的工作是不可能的 但是,如果你足够努力地搜索,你可能会找到一种方法 使用Doctrine 1.1,您可以扩展模型类Doctrine\u Record。 该类定义了此方法: /** * Get the record error stack as a human readable string. * Useful for outputting errors to user via we

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

在当前版本中不可能

在某种程度上是正确的:没有一些艰苦的工作是不可能的

但是,如果你足够努力地搜索,你可能会找到一种方法


使用Doctrine 1.1,您可以扩展模型类
Doctrine\u 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\u-doctor\u-Record
    ,它扩展了
    doctor\u-Record
  • 该类将重新定义该方法,以允许自定义错误消息
  • 我们的模型类将扩展这个
    My\u doctor\u Record
这将避免在我们的每个模型类中重复该方法;并且可能有一天会证明它是有用的


当然,我们的
My_Doctrine\u 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');
我猜你的应用程序中已经有类似的东西了


接下来,我们的新
我的条令记录
课程:

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
  • 我们可以在模型类中重载
    \u 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_document\u Record
    ,并定义了一个
    \u getValidationFailed
    方法,该方法处理我的模型的
    name
    字段上的验证错误


    现在,假设我这样做是为了加载一条记录:

    $test = Doctrine::getTable('Test')->find(1);
    var_dump($test->toArray());
    
    让我们尝试修改它,设置“坏”值:

    您可以看到“
    名称
    ”的消息已经定制,而“
    ”的消息来自默认的规则


    因此,总结一下:不容易,但一定要做到

    现在,您可以将此作为问题确切解决方案的指南:-)
    它需要更多的编码,我想。。。但你离真正的交易不远了


    玩得开心

    胡,我昨天刚看到你在信条的邮件列表上贴了这个问题:-D哦,至少,jwage同意疯狂乔的观点^^
    $test->name = (string)time();
    $test->value = 'glop';
    try {
        $test->save();
    } catch (Doctrine_Validator_Exception $e) {
        echo '<pre>';
        echo $e->getMessage();
        echo '</pre>';
        die;
    }
    
    BAD DATA in class Test :
        * You entered wrong data from 'name' field.
          Errors are for 'email'
        * 1 validator failed on value (htmlcolor)