1

I'm just getting started on using MVC patterns in my Java applications. I've been using MVC a bit in CodeIgniter before, but I want make sure I'll get it right.

  • The View is the GUI files, which sends inputs to the controllers.

  • The controllers then receives these inputs, and eventually send them on to the model.

In my CodeIgniter applications, all I used the Models for were database related stuff. If I need to validate an e-mail for example, should I do it in the Controller or in the Model?

4
  • I'm not an expert on MVC, but to me it seems like a controller task, because the Controller's job is to convert input from the view to what the model can understand. So validating the user input and sanitizing it so that the model can use/send/parse/whatever it sounds like 'Controller'. Commented Feb 15, 2013 at 9:31
  • I removed your last paragraph, in the hope of keeping this question from being closed as "not constructive". Commented Feb 15, 2013 at 9:31
  • And note you are using Apple's version of MVC, it differs slightly from the original version: oracle.com/technetwork/articles/javase/mvc-136693.html (you have to scroll to 'modifying the MVC design, or Ctrl+F and then 'Apple'). Commented Feb 15, 2013 at 9:35
  • You might want to consider Model-View-Presenter (MVP) as an alternative. I prefer it, personally, because it pushes all the business logic into the Controller. Commented Feb 15, 2013 at 10:38

3 Answers 3

3

Where to do validation is a question which is still open to discussion in the MVC pattern.

Some trivial validations can and should be done on the view. Example: a text input widget which takes a numerical value shouldn't even allow the user to input letters. It doesn't have to consult controller or model to do that.

Some say it's the controllers job to ensure that it doesn't feed garbage to the model because deciding what's valid and what's not is part of the business logic.

Some say it's the models job to ensure its own consistency, so it should validate everything which comes from the controller and reject any garbage data.

tl;dr: It depends.

Sign up to request clarification or add additional context in comments.

Comments

1

Client side validation must be incorporated in the View while the server side validation regarding the scenario like yours, Controller is best suitable for the job. Model must be used only to do some business logic stuff and the database processes.

Comments

0

From the MVC model the

  • View is responsible for acting an interface/end point between you app and the exterior, most often it's a GUI of some sort
  • The controller represents the dispatching part, and should be kept pretty light
  • the Model treats you business logic, talks to the DB, etc.

Regarding you question about email validation, you should do it in two places, in the View/GUI via javascript, to ensure a quick and pretty feedback in the case the user enters a bad email address, and also to spare you the useless hits. But apart from this you must also do it somewhere server side. The actual logic for this would be in the Model layer, a method like isValidEmail(String emailAddress) that returns a boolean and is declared in the Model somewhere and gets called from the controller. I was Thinking of something along the lines of:

    public void myAction() {
       //we are in the controller
       if(!Manager.isValidEmail(emailAddressAsString)) {
          dispatchBadEmailView(); //dispatch to a bad email address view
       }
    }

This is my opinion, and also if you didn't already, try to use Spring MVC or something like this, it helps alot, read more here: http://www.mkyong.com/tutorials/spring-mvc-tutorials/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.