Php 7 的 Yii2 自定义验证问题

Yii2 Custom validation problem with Php 7

我正在使用 PHP 版本 5.6.25 和 Yii2 版本 2.0.15.1,并且可以正常使用自定义验证,因为我将 php 版本更改为 7.0.10,模型给出以下错误

{"name":"PHP Notice","message":"Array to string conversion","code":8,"type":"yii\base\ErrorException","file":"D:\wamp\www\cfms-hc\models\CASES.php","line":210,"stack-trace":["#0 D:\wamp\www\cfms-hc\vendor\yiisoft\yii2\validators\InlineValidator.php(72): ::call_user_func:{D:\wamp\www\cfms-hc\vendor\yiisoft\yii2\validators\InlineValidator.php:72}()","#1 D:\wamp\www\cfms-hc\vendor\yiisoft\yii2\validators\Validator.php(267): yii\validators\InlineValidator->validateAttribute()","#2 D:\wamp\www\cfms-hc\vendor\yiisoft\yii2\base\Model.php(367): yii\validators\Validator->validateAttributes()

我的验证函数如下。

public function validateInstitutiondate($attribute,$params)
{
        $institutiondate = date('Y', strtotime($this->$attribute));
        //$institutiondate = $this->$attribute ;
        $caseyear = $this->$params['CASEYEAR'];
    //$aa = $params['CASEYEAR'];
    //$caseyear = $this->$params->CASEYEAR;
    if ($institutiondate != $caseyear) {

        $this->addError($attribute, 'Institution date must be of same year.'.$caseyear);
        //$this->addError($this, $attribute, '{attribute} must be of same year.');
        return false;

    }

}

并使用以下行进行验证。

 ['INSTITUTIONDATE','validateInstitutiondate','params'=>['CASEYEAR'=>'CASEYEAR']],

这与 PHP 7.0 中引入的间接属性的优先级更改有关。在PHP 5 中,$this->$params['CASEYEAR'] 被视为$this->{$params['CASEYEAR']},但在PHP 7 中,它被视为$this->{$params}['CASEYEAR']。您需要使用 {} 指定应如何处理表达式:

$caseyear = $this->{$params['CASEYEAR']};

upgrade notes about this change