CodeIgniter Forums
Create Model with command line - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Create Model with command line (/showthread.php?tid=83417)



Create Model with command line - [email protected] - 09-24-2022

Hi..
I created model from command line and when i open the model file to edit there is this part which i don't understand.

PHP Code:
protected $cleanValidationRules true

Can anyone give explanation or point out the documentation on what is it and how to use that part?
I've tried search on the userguide but found nothing, or maybe i wasn't looking carefully because i only use the search input with cleanValidationRules text.



RE: Create Model with command line - InsiteFX - 09-25-2022

I had to look at the system/BaseModel to find it.

PHP Code:
/**
    * Whether rules should be removed that do not exist
    * in the passed in data. Used between inserts/updates.
    *
    * @var bool
    */
    protected $cleanValidationRules true



RE: Create Model with command line - kenjis - 09-25-2022

It seems the functionality is not documented.

See the source code.
https://github.com/codeigniter4/CodeIgniter4/blob/47ed563468e0887d4a9bcdc770de429dbca252ab/system/BaseModel.php#L1340


RE: Create Model with command line - [email protected] - 09-25-2022

Thanks @kenjis , @InsiteFX
So looking at your pointers does it means that if set to true then whenever we pass request data on model for easy to insert/update it will only validate only matching field/rules name on request data?
Does it also works not only on model but on every time we use validation library? I was thinking if i can make that some global validation that have all rules saved on validation config or separate files.
Sorry for my bad english.


RE: Create Model with command line - kenjis - 09-25-2022

If $cleanValidationRules is true, the validation rules for the field that is not contained in the passed data will be removed before validation.

But it does not seems to work if you don't call validate() by yourself.
If you call insert() or update() or save(), the $cleanValidationRules value will be changed automatically.
I'm not sure this is a bug or not.

Anyway, it is a feature in Model, not in the Validation library.


RE: Create Model with command line - [email protected] - 09-25-2022

(09-25-2022, 02:20 PM)kenjis Wrote: If $cleanValidationRules is true, the validation rules for the field that is not contained in the passed data will be removed before validation.

But it does not seems to work if you don't call validate() by yourself.
If you call insert() or update() or save(), the $cleanValidationRules value will be changed automatically.
I'm not sure this is a bug or not.

Anyway, it is a feature in Model, not in the Validation library.
Yes, i think it is a bug too. I tried and get the same result also. There would be no use to put $cleanValidationRules in model if it gets replaced automatically on calling model insert/update/save. The only way it would works if we capture the validation error on validate() then stop executing next line.
Is there any other workaround?


RE: Create Model with command line - kenjis - 09-25-2022

(09-25-2022, 08:23 PM)[email protected] Wrote: Yes, i think it is a bug too. I tried and get the same result also. There would be no use to put $cleanValidationRules in model if it gets replaced automatically on calling model insert/update/save.

Agreed.
Okay, I will fix it as a bug.


RE: Create Model with command line - kenjis - 09-26-2022

I sent a PR.
https://github.com/codeigniter4/CodeIgniter4/pull/6588


RE: Create Model with command line - InsiteFX - 09-26-2022

Thank you @kenjis


RE: Create Model with command line - [email protected] - 09-26-2022

Thank you all for helping me