Same form, different scenarios, unsafe attributes

Hi, all!

I have a form with 3 fields and 2 scenarios.

Here’s my model rules:





// DailyReport model


public function rules()

{

    return array(

        array(

            'date',

            'required',

            'on' => 'reportByDate'

        ),

        array(

            'date',

            'date',

            'format'=>array('yyyy-MM-dd'), 

            'message' => 'Bad date format. Should be yyyy-mm-dd.',

            'on' => 'reportByDate',

        ),

        array(

            'startDate, endDate',

            'required',

            'on' => 'reportByDateRange'

        ),

        array(

            'startDate, endDate',

            'date',

            'format'=>array('yyyy-MM-dd'), 

            'message' => 'Bad date format. Should be yyyy-mm-dd.',

            'on' => 'reportByDateRange',

        ),

    );

}



When I submit form with ‘date’ attribute set and create model like so


$model = new DailyReport('reportByDate');

I get 2 warnings:


Failed to set unsafe attribute "startDate" of "DailyReport"

Failed to set unsafe attribute "endDate" of "DailyReport"

And vice versa, when I submit form with ‘startDate’ and ‘endDate’ attributes set and create model like so


$model = new DailyReport('reportByDateRange');

I get 1 warning:


Failed to set unsafe attribute "date" of "DailyReport"

Now, as far as I understand when a model’s attribute is not assigned a validator or not explicitly marked by ‘safe’ for a particular scenario - it’s not assigned during massive assignment.

But my question is: does it mean that unsafe attributes for current scenario will always produce this warning in log file? I guess there will be too many warnings if this is the way it is.

This warning will not be logged if YII_DEBUG is set to false. So the purpose is to make debugging/development easier, i.e.: log message vs. silently ignore unsafe attributes

Ah, then it’s cool.

Thanks a lot, Y!!

It is also possible to override onUnsafeAttribute in the model if you don’t want Yii to log a warning when YII_DEBUG is true:




public function onUnsafeAttribute($name, $value) {

    //silently ignore unsafe attributes

}



Ref. http://www.yiiframework.com/doc/api/1.1/CModel#onUnsafeAttribute-detail