Welcome Guest, Not a member yet? Register   Sign In
Form processing in the controller or the model...best practices?
#1

[eluser]Unknown[/eluser]
I'm working on my first CI application (my first MVC app as well). My natural instinct is to keep my controllers as simple as possible and put most of the processing in my models, this includes form processing and validation.

So my controller method for a simple CRUD add form looks like this:

Code:
public function add()
    {
        if ($this->input->post('submit'))
        {
           if ($this->assignments_model->add_assignment())
            {
                redirect('assignments');
            }
            else
            {
                $this->load->view('assignments/add_view');
            }
        }
        else
        {
            $this->load->view('assignments/add_view');
        }
    }

The model methods look like this:

Code:
function add_assignment()
    {
        if ($this->validate_form())
        {
            $this->construct_from_form ();
            $this->created  = date ('c', now());

            $this->db->insert('assignments', $this);
            return TRUE;
        }
        
        return FALSE;
    }

function construct_from_form ()
    {
        $this->name = $this->input->post('name');
        $this->description = $this->input->post('description');
        $this->due_date = shortdate_to_mysql($this->input->post('due_date'));
        $this->points = $this->input->post('points');
        $this->maxfiles = $this->input->post('max');
        $this->replace = $this->input->post('replace');
    }

function validate_form ()
    {
        $this->form_validation->set_rules('name', 'assignment name', 'trim|required|max_length[40]');
        $this->form_validation->set_rules('max', 'max files', 'trim|integer');
        $this->form_validation->set_rules('points', 'points', 'trim|integer');
        $this->form_validation->set_rules('replace', 'replace', '');
        $this->form_validation->set_rules('description', 'description', 'max_length[400]');
        $this->form_validation->set_rules('due_date', 'due date', 'trim');

        return $this->form_validation->run();
    }

This all works fine, but it seems like I see most of the form processing and validation done in the controller in the tutorials I've seen (including some of the examples in the user guide).

So my question is, am I breaking MVC best practice techniques with this approach? Is this an inefficient or ugly solution?

Appreciate any input, thanks.
#2

[eluser]John_Betong_002[/eluser]
As far as I know there are different schools of thought and some interesting blogs available.

I have adopted the practise of treating the model as if written by a third-party vendor.

The model function returns a result depending on the information provided.

The controller acts upon the returned result and chooses a particular direction.

From bitter experience I am finding that it is best to have multiple models because as the project matures the model soon becomes difficult to manage.
 
 
 
#3

[eluser]InsiteFX[/eluser]
I do all my forms in a library with its own view! This way I do not have to keep re-writing my forms
once they are finished and debugged I add them to my forms library.

But there are all kinds of different ways of doing it!

I like to think Re-useable Code!

InsiteFX
#4

[eluser]jppi_Stu[/eluser]
I'm not exactly an M-V-C guru, but at the conceptual level, if you look at the model as being just a data model, it's not the place for anything related to the user experience, including interactive views (forms) and non-interactive views (reports/pages). The view handles the actual user interface, but only based on what the controller has instructed. The controller is in control; it requests things from the model or sends things to the model for storage, handles whatever processing is needed, and provides instruction to the view for what to display to the user.

Just as I would not use a stored procedure in SQL to handle a data entry form in a stand-alone application, I would not put form processing in a CI Model. The model would not be involved until the controller had taken care of everything other than storing the data. Only then would the controller pass the data off to the model to be stored.

Of course CI is flexible in that there are multiple ways of doing things, but you were asking for best practices, and I think treating the model as just an interface to the back-end data is the conceptually correct approach. Form validation is processing user input, before we can even know if the data is worth storing, so it's none of the model's business, so to speak.

I think this abstraction serves code reuse. If you make the model do form validation, you're tying a method of the model object to that form (or set of equal forms). If you add a different form, you have to create a new method to handle it. This means you're changing the model to match changes in the UI, which is a bad road to be on -- it can lead to more duplication, regression, etc. If it's abstracted to just handle data, you can design the object interface to be reusable by many different forms, pages, etc. without ongoing changes to the model. Once properly defined, the model should only be changed if the back-end data is changed.

That's my take on it, FWIW...




Theme © iAndrew 2016 - Forum software by © MyBB