• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Way to have validation code in the model?

Is there any code examples of how to keep code validation for fields within the model?

If you give us some background on what you're trying to do, we will be able to understand your rationale for wanting to do this and try to come up with a solution. Right now, it's a little difficult to understand what you're really asking.

Asking to get validation rules and fields in model just prevent you to always type the same stuff in controller, a field is still a field to the database.

This is usually how is presented MVC stuff.

keep me posted if someone work on something..

I do this for the exact reasons you said. I have a validate function in the model. In the controller, whenever a form is submitted, I load the model and call the validate function.

I don't use CI's Validation feature, but the concept is the same. This should be straightforward to do.

The only code I have in a model is code to talk to the database. So I just pass all the form data to the model and then it deals with it. Also when getting things from the database I put it in the format I want in the model.

I don't know why you would have validation functions in the model? That's not what its for. If a controller submitted the data then maybe have a method in the controller to handle it. Why not use the validation class? Its very powerful.

Adamp1 : no no, in fact this is how it's suppose to be handled in real MVC. Most advanced framework will do validation into model, because it's not logic to write validation stuff two time, by example, edit, create.

I mean the validation class just work great, but it's hard to find an elegant way to DRY the code with it.

Ok so how do you go about setting up validation in the model? And calling it from the controller. What about re-populating the form after validation has failed or populating it for the first time (IE a modify form).

I'm fully open to suggestions to making my applications better, and what do you mean DRY the code with it?

[quote author="adamp1" date="1204571599"]I don't know why you would have validation functions in the model? That's not what its for.[/quote]

I think you are misinformed. You probably think the concept of a Model is to only connect to a database. In fact, the model's purpose is to manage any kind of data. Why would you validate a submission (to the database) in the Controller, when it's purpose is merely to direct traffic/load content?

It is perfectly logical to do validation within the model, because the validation concerns the data that is sent to the database. The work involved with creating, fetching, deleting, and validating of data should be all done within the model.

A very simple example using from the Controller:

class Controller
    public function submit()
        $errors = $this->Some_Model->validate($data);
        $this->load->view('form' , $errors);

OK but does someone have a small example I can look at. I can't see straight away how it would all be handled. At the moment I can only see my method, but would change if it meant my app with more modular and made more sense.

At the moment, i don't have any easy way to handle it with Ci in his current structure.

This is just the point to handle this :

$files, $rules, fill up ->validation, handle callback, get back to controller.

Also take not, that i usually handle email stuff in my model.

Thanks !

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.