Welcome Guest, Not a member yet? Register   Sign In
How develop Model and validation
#1

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
Reply
#2

Better to do it in in controller. You model can then just process the data.
Reply
#3

(01-12-2020, 04:20 PM)deltatangodt Wrote: Better to do it in in controller. You model can then just process the data.
Yes i think so too
Thanks
Reply
#4

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
Reply
#5

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
Reply
#6

(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.

Hope this help

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.
Reply
#7

(This post was last modified: 01-15-2020, 01:05 AM by omid_student.)

(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.

Here's an example how it's done in Codeigniter 4:
https://codeigniter4.github.io/userguide...ating-data
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.

Hope this help

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.

Here's an example how it's done in Codeigniter 4:
https://codeigniter4.github.io/userguide...ating-data

I see relate link
It is good way thanks
Reply
#8

They should return a Boolean false or true.
Reply
#9

(01-15-2020, 12:36 PM)jreklund Wrote: They should return a Boolean false or true.

Of course
Reply
#10

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 )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB