CodeIgniter Forums
Best way to implement validation rules - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Best way to implement validation rules (/showthread.php?tid=78585)



Best way to implement validation rules - falcon812311 - 02-10-2021

Hi, 

I'm developing a system for large scope project. I'm very confuse with validation in the CodeIgniter guide/documentation  it is hard to understand.

What is the best strategy to store validation rule?
Use Validation service or Controller or Models?


Huh


RE: Best way to implement validation rules - jasonzig - 02-10-2021

(02-10-2021, 07:33 AM)falcon812311 Wrote: Hi, 

I'm developing a system for large scope project. I'm very confuse with validation in the CodeIgniter guide/documentation  it is hard to understand.

What is the best strategy to store validation rule?
Use Validation service or Controller or Models?


Huh

If you referring to data-entity validation during db INSERT/UPDATE/DELETE operations,
the setValidationRule method is mentioned in the docs (https://codeigniter.com/user_guide/models/model.html?highlight=validation#setValidationRule) as taking place in the model.

Please keep us updated on how your project goes!

Incidentally, how did you select CodeIgniter4?


RE: Best way to implement validation rules - adnzaki - 02-10-2021

I used to store validation in controllers. I just create a method containing rules and messages, so it can be used by one or more methods. I usually make a separation between the app and database by its controller and model. Every data should pass validation in controller before they sent to model to be inserted to database.


RE: Best way to implement validation rules - falcon812311 - 02-10-2021

(02-10-2021, 12:01 PM)jasonzig Wrote:
(02-10-2021, 07:33 AM)falcon812311 Wrote: Hi, 

I'm developing a system for large scope project. I'm very confuse with validation in the CodeIgniter guide/documentation  it is hard to understand.

What is the best strategy to store validation rule?
Use Validation service or Controller or Models?


Huh

If you referring to data-entity validation during db INSERT/UPDATE/DELETE operations,
the setValidationRule method is mentioned in the docs (https://codeigniter.com/user_guide/models/model.html?highlight=validation#setValidationRule) as taking place in the model.

Please keep us updated on how your project goes!

Incidentally, how did you select CodeIgniter4?

I love CodeIgniter4 because it is easy to understand and fast! This is my website (https://jaironlanda.com) I'm develop using latest version CodeIgniter 4. 

Unfortunately, the useguide is hard to understand. It is hard to find the right way to use validation, library, custom model, and etc. Sometime I refer from youtube (https://www.youtube.com/channel/UCIbtEs1nrFY14bgD_ake7HA) he is good, he show example how use and how its work!

So back to my question about validation rule. I wonder when to use Validation in Models, Validation in Controller, and Validation service?

(02-10-2021, 01:07 PM)adnzaki Wrote: I used to store validation in controllers. I just create a method containing rules and messages, so it can be used by one or more methods. I usually make a separation between the app and database by its controller and model. Every data should pass validation in controller before they sent to model to be inserted to database.
 
How you make separation between the app and database? you put all validation rules here? 
PHP Code:
Config\Validation.php 

Thanks!


RE: Best way to implement validation rules - paliz - 02-11-2021

(02-10-2021, 07:33 AM)falcon812311 Wrote: Hi, 

I'm developing a system for large scope project. I'm very confuse with validation in the CodeIgniter guide/documentation  it is hard to understand.

What is the best strategy to store validation rule?
Use Validation service or Controller or Models?


Huh
use both of them