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.php
$user=new User();
$user->username='xxxx',
$user->avatar='/xx/xx/avatar.jpg';
$user->address='xxxxxx street xxx';
$user->save(false);
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();
$user->attributes=[.....]
if($user->save()){
return true;
}else{
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.
2 Likes
Thanks, solved a problem I’ve been wrestling with for a long time.
1 Like