Yii2 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']],


莫回无
浏览 163回答 1
1回答

慕桂英3389331

这与PHP 7.0中引入的间接属性的优先级更改有关。在PHP 5中$this->$params['CASEYEAR']被视为$this->{$params['CASEYEAR']},但在PHP 7中则被视为$this->{$params}['CASEYEAR']。您需要使用{}指定表达式的处理方式:$caseyear = $this->{$params['CASEYEAR']};请参阅有关此更改的升级说明。
打开App,查看更多内容
随时随地看视频慕课网APP