【问题标题】:yii2 rewrite model addErroryii2 重写模型 addError
【发布时间】:2017-11-30 09:16:26
【问题描述】:

这是原来的Model.php函数

public function addError($attribute, $error = '')
{
    $this->_errors[$attribute][] = $error;
}

在我的模型属性中,我想以这种方式覆盖此方法

public function addError($attribute, $error = '')
{
    if ($attribute == 'status'){
        $this->_errors[$attribute] = $error;
    }
    else{
        $this->_errors[$attribute][] = $error;
    }
}

我无法访问 $this->_errors ... 我不知道如何在不修改主 model.php 的情况下覆盖此功能

【问题讨论】:

  • 您是否在子类中扩展了原始 Model.php 类??

标签: php yii yii2 yii2-advanced-app yii2-validation


【解决方案1】:

找到解决方案! 在个人模型类中添加这个

/**
 * @param array $errors
 */
public function setErrors($errors)
{
    $this->errors = $errors;
}

在我的验证函数中

$this->setErrors(['status' => false, $attribute => 'not found']);

不使用addError函数

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多