Welcome Guest, Not a member yet? Register   Sign In
Form validation not in controller but rather in Model

Hi all,

just a very simple question (maybe stupid, but I try...)

Must we put the form_validation in a controller's method or could we put it in a model class ?
I mean, till now I do this kind of things:
I have a form, in the form the action on submit call a controller method in which we can check the post data.

But I'm in a case where this kind of design is weak, moreover dealing with data checking in a controller is not perhaps the good manner to do.

Then does it work if In my controller method I call $this->my_model->checkData(list of data posted)
HoweverI wonder also what appear for the error message displaying with the form_validation (I load it in the model)
Thanks for your help

moreover dealing with data checking in a controller is not perhaps the good manner to do.

May it's u'r personal style of working or it's logic that need such kind of data flow... but, as far as MVC approach concern, CONTROLLER is made for controlling any kind of checking & validation in the 1st entry stage of the application. Then, after checking it's CONTROLLER should be decide what kind of action taken over the request it has received. In this way it's act like a protective shield to any application.

About MODEL, it's a section which directly work with DB. So, may be there are chances if any thing will go wrong then it's effect directly on u'r DB data.

So in my suggestion , if u'r application has no specific necessary which need form validation at MODEL side then complete all those jobs in controller side.

Thank you mi6crazyheart for your reply.

I do not agree at all.

My mind Model has to deal with data checking rules (Business rules).
For instance, in my form if I provide 2 dates, and internally if I have a rule that says :'the two dates must be separate at least by xx days'
Then for me this kind of thing must be verify in model. It's a business rule.

Controller has to collect data from the form, and transmitting them to the model.

A model is also not absolutly dedicated to a db, for instance contact form are not connected to db.

But OK, I imagine that people may place their check wherever they want (controller or model) but I wonder if doing this stuff on model, we can have the error messages issued from 'form_validation' in the Form field (in the case where user enter wrong data).
I fear that with CI approach It couldn't work.

Ok, if this is the matter of business logic then i agree with u. I also believe it's, MODEL is the right place to deal with business logic.

And about u'r current problem , i want to give u suggestion...
Do what ever form validation in u'r controller side and do the comparing between two data part at the side of MODEL & return back the result to controller & load the appropriate view according to returned result from MODEL.

Try it if u feel it's ok with u'r logic... Smile

Good suggestion, mi6crazyheart
I'll do it


Theme © iAndrew 2016 - Forum software by © MyBB