CodeIgniter Forums
Custom Validation Rule in Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Custom Validation Rule in Model (/thread-78458.html)



Custom Validation Rule in Model - josh2112o - 01-22-2021

I know we can have custom validation rules in the controller, by adding more rule sets to the /Config/Validation.php file but how do we add them to the model when performing validation there.
Something like:

PHP Code:
protected $validationRules = ['myfield'  => 'trim|required|customRule(myfield')]; 
Is it possible to call in the Model like this and how do you include any custom validation libraries in the model, if possible?
Thanks for any help and suggestions.


RE: Custom Validation Rule in Model - iRedds - 01-22-2021

I didn't fully understand your problem.
You can override the validate() method of the model to include your library.
Or add a method with your library to the beforeInsert/update/delete events.


RE: Custom Validation Rule in Model - josh2112o - 01-22-2021

(01-22-2021, 02:23 PM)iRedds Wrote: I didn't fully understand your problem.
You can override the validate() method of the model to include your library.
Or add a method with your library to the beforeInsert/udpate/delete events.
Hi,
Thanks for your response...

I was hoping to use the validation functionality in the model itself instead of the Controller. I have written some validation rules for controller but want to write and use some for use in the model along with those built-in validations (E.g., required|in_list etc).

How do I add a method from my library to the beforeInsert/udpate/delete events?

Thanks for your help.


RE: Custom Validation Rule in Model - iRedds - 01-22-2021

(01-22-2021, 04:08 PM)josh2112o Wrote: I was hoping to use the validation functionality in the model itself instead of the Controller. I have written some validation rules for controller but want to write and use some for use in the model along with those built-in validations (E.g., required|in_list etc).

https://codeigniter.com/user_guide/models/model.html#validating-data

(01-22-2021, 04:08 PM)josh2112o Wrote: How do I add a method from my library to the beforeInsert/udpate/delete events?

https://codeigniter.com/user_guide/models/model.html#model-events


RE: Custom Validation Rule in Model - josh2112o - 01-22-2021

(01-22-2021, 05:26 PM)iRedds Wrote:
(01-22-2021, 04:08 PM)josh2112o Wrote: I was hoping to use the validation functionality in the model itself instead of the Controller. I have written some validation rules for controller but want to write and use some for use in the model along with those built-in validations (E.g., required|in_list etc).

https://codeigniter.com/user_guide/models/model.html#validating-data

(01-22-2021, 04:08 PM)josh2112o Wrote: How do I add a method from my library to the beforeInsert/udpate/delete events?

https://codeigniter.com/user_guide/models/model.html#model-events
Hi,
Thanks for the links...
So I have this code in my model for validation:
PHP Code:
protected $validationRules = [
 
'day_walked' => 'trim|required|is_unique[walking_stats.day_walked]',
'cal_burned' => 'trim|required|decimal|greater_than[0]',
'miles_walked' => 'trim|required|decimal|greater_than[0]',
'duration' => 'trim|required|exact_length[8]|durationFormat(duration)|durationFormatValues(duration)|durationZeroError(duration)',
'mph' => 'trim|required|decimal|greater_than[0]',
  ]; 

Say for the
PHP Code:
durationFormat(duration
rule, if I have that function called out in the public $ruleSets array in the Config/Validation.php file which is written in my app/Validation/WalkValidationRules.php file, can I access that function and other's in in my model?


RE: Custom Validation Rule in Model - iRedds - 01-22-2021

(01-22-2021, 05:54 PM)josh2112o Wrote: Hi,
Thanks for the links...
So I have this code in my model for validation:
PHP Code:
protected $validationRules = [
 
'day_walked' => 'trim|required|is_unique[walking_stats.day_walked]',
'cal_burned' => 'trim|required|decimal|greater_than[0]',
'miles_walked' => 'trim|required|decimal|greater_than[0]',
'duration' => 'trim|required|exact_length[8]|durationFormat(duration)|durationFormatValues(duration)|durationZeroError(duration)',
'mph' => 'trim|required|decimal|greater_than[0]',
  ]; 

Say for the
PHP Code:
durationFormat(duration
rule, if I have that function called out in the public $ruleSets array in the Config/Validation.php file which is written in my app/Validation/WalkValidationRules.php file, can I access that function and other's in in my model?

Sorry, I still don't understand what you want  Blush
Can you explain in more detail with an example?

If you need to access the durationFormat() method in the model, then simply create an instance of the WalkValidationRules class.

PHP Code:
$wvr = new \App\Validation\WalkValidationRules();
$wvr->durationFormat(); 



RE: Custom Validation Rule in Model - josh2112o - 01-23-2021

(01-22-2021, 09:24 PM)iRedds Wrote:
(01-22-2021, 05:54 PM)josh2112o Wrote: Hi,
Thanks for the links...
So I have this code in my model for validation:
PHP Code:
protected $validationRules = [
 
'day_walked' => 'trim|required|is_unique[walking_stats.day_walked]',
'cal_burned' => 'trim|required|decimal|greater_than[0]',
'miles_walked' => 'trim|required|decimal|greater_than[0]',
'duration' => 'trim|required|exact_length[8]|durationFormat(duration)|durationFormatValues(duration)|durationZeroError(duration)',
'mph' => 'trim|required|decimal|greater_than[0]',
  ]; 

Say for the
PHP Code:
durationFormat(duration
rule, if I have that function called out in the public $ruleSets array in the Config/Validation.php file which is written in my app/Validation/WalkValidationRules.php file, can I access that function and other's in in my model?

Sorry, I still don't understand what you want  Blush
Can you explain in more detail with an example?

If you need to access the durationFormat() method in the model, then simply create an instance of the WalkValidationRules class.

PHP Code:
$wvr = new \App\Validation\WalkValidationRules();
$wvr->durationFormat(); 
I'll try that. Again, I want to keep all validation in the model and use some custom rules I have written. Thanks for your help.