tags:

views:

17

answers:

1
  1. Say you have a User model. The controller is attempting to create a new User. Should the controller check that the username is valid, and the password is long enough, and the first and last name are filled out, etc? Or should you pass all that data straight to the User model via a Create method? The Create method would then return a true on success, or false on failure?

  2. If it's the latter (and I think it is), how do the error messages get sent back to the controller (so they can be displayed in a view)? Should you pass an errors array to the Create method which the model augments? Or should the model keep an internal store of errors, with appropriate accessors? I don't like either method...is there a better way?

These errors don't seem exceptional, so I don't think exception handling is appropriate.

Edit: I'm using PHP for this project, but I use Python too.

+1  A: 

For the first question, the model should do the verifications (and use some form of error handling to notify the controller and view that errors did or did not occur). For the second, it depends on what programming language / framework you are using... What are you using?

Kevin Sylvestre
I use PHP or Python + Django
Matt
Add an instance variable on your model containing all validation errors (a set or array). Then, in your "create" method, add the errors to this instance variable as you see fit. In the controller, you can check for "errors" and display the correct view. In the view, you can further check for errors to display inline messages. In Django, see: http://docs.djangoproject.com/en/dev/ref/validators/#ref-validators.
Kevin Sylvestre