How develop Model and validation |
Hi
In this my project(Shop),there are 4 model (User,Shop,Order,Category) There is no problem I want to know only,is it correct validate function argument in model or i have to validate it in controller? Example I have User model with Login function This function have to arguments(username,password) Do i have to check username and password in Model or in Controller validate and then use model? Actually i want to know what is standard programming for use model and validation and split this from their
I think bettet create service class for handling logic process. You can use multiple model (example save log history), and also if you want to create REST Api login, you just call this service class. You can prevent duplicating code for login. Read ADR pattern.
Hope this help
This depends on the person codeing it. If you are doing all validation in the Controller, you you need to duplicate it in every Controller. If you are doing it in the Model, you only need to do it ones.
Here's an example how it's done in Codeigniter 4: https://codeigniter4.github.io/userguide...ating-data (01-13-2020, 08:33 AM)asrofie Wrote: I think bettet create service class for handling logic process. You can use multiple model (example save log history), and also if you want to create REST Api login, you just call this service class. You can prevent duplicating code for login. Read ADR pattern. I would also second that because if I were you, I would do that because then service class would make my work easier.Also, it is easier to prevent duplication of login code. But then again, it really depends on you.
(01-13-2020, 11:49 AM)jreklund Wrote: This depends on the person codeing it. If you are doing all validation in the Controller, you you need to duplicate it in every Controller. If you are doing it in the Model, you only need to do it ones.I agree with you But in model,what to return to controller? Example: if (!'/\d+/',$mobile) return 'invalid'; or return -1; What one is correct? Thanks (01-13-2020, 08:33 AM)asrofie Wrote: I think bettet create service class for handling logic process. You can use multiple model (example save log history), and also if you want to create REST Api login, you just call this service class. You can prevent duplicating code for login. Read ADR pattern. Do you mean Hook? (01-13-2020, 11:49 AM)jreklund Wrote: This depends on the person codeing it. If you are doing all validation in the Controller, you you need to duplicate it in every Controller. If you are doing it in the Model, you only need to do it ones. I see relate link It is good way thanks
Always return true or false boolean that is the correct way of doing it.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |