Waseem Hassan
Waseem Hassan

Reputation: 45

Yii2 Custom validation problem with Php 7

I am using PHP version 5.6.25 with Yii2 version 2.0.15.1 and working fine with custom validation, as i change my php version to 7.0.10 the model gives following error

{"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()

My validation function is following.

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;

    }

}

and validating using following line.

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

Upvotes: 0

Views: 95

Answers (1)

rob006
rob006

Reputation: 22174

This is related to change in precedence of indirect properties introduced in PHP 7.0. In PHP 5 $this->$params['CASEYEAR'] is treated as $this->{$params['CASEYEAR']}, but in PHP 7 it is treated as $this->{$params}['CASEYEAR']. You need to use {} to specify how expression should be treated:

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

See upgrade notes about this change.

Upvotes: 2

Related Questions