I would like to ask, do you need to go through rules verification for non-form submission and modification of model data?

I know that when submitting data, the client should take rules verification to save the data. If the model data is modified within the business code, do you also need to take rules to save the data? This can ensure the uniformity of data, how to recommend it?

Validation is required as business code might have errors that can result in incorrect data like referencing wrong foreign keys that does not exist in db etc. It is not recommended to turn off the validation but you can turn off validation by $model->save(false);. it will ignore the model rule validation while saving to DB.

1 Like

In business code, has a similar operation,Instead of submitting the form through the client, the business code manually adds users.

$user=new User();
$user->address='xxxxxx street xxx';

Although the probability of error in the above code’ attribute’ is very low, the possibility of error still exists. Should we take the rule verification channel?

$user=new User();

    return true;
    return false;

Ensure the accuracy of data as much as possible?

I would suggest to keep validation rules on, business code should be able to pass the validation.


Thanks, solved a problem I’ve been wrestling with for a long time.

1 Like