Welcome Guest, Not a member yet? Register   Sign In
Validating in Controller vs Model
#1

[eluser]animatora[/eluser]
Hi all, I recently bought this book of Jamie "Codeigniters hand book". There he is making some very good observations about how should a model and controller look like. Has anyone come across his Model implementation on github, I can't understand how to make data validation.

Lest say I have this in my controller:

Code:
public function create()
{

$data = array(
'email' => $this->input->post('email'),
'first_name' => $this->input->post('first_name')
);

// How to perform insertion here, so I can send back to my view success or error messages
// $this->model_name->create($data);
// OR I have to run validation from the model and then insert data.
}

My model has all the validation setup in the crete method I just call the super insert like:

Code:
public function create($data)
{
return $this->insert($data);
}

Any help is appreciated
#2

[eluser]Aken[/eluser]
I do form validation in the controller, and use try/catch with exceptions in my models.
#3

[eluser]WanWizard[/eluser]
For data that is destined for a model, I always validate in the model.

Advantage of this is that no matter where your data comes from, the model makes sure it's valid before it's inserted into the database. It also means a single location for validation, instead of in every controller that does something with data that is input for the model.

Jamie's model absolutely makes sense.

As to messaging, I use a central messaging class that collects all messages, categorises them, and stores them in the session if needed. It also has a method for retrieval, which is used by the partial I use to display messages on the page.
#4

[eluser]RS71[/eluser]
WanWizard, how do you approach model validation? I'm trying to validate models, some with lots of fields and it drives me nuts -- I have a bunch of redundant code and calls.

Isn't the method of overwriting POST data a bit iffy in Jamie's model?




Theme © iAndrew 2016 - Forum software by © MyBB