我将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']],
慕桂英3389331