开发者

How to get error messages from model

开发者 https://www.devze.com 2023-01-03 23:02 出处:网络
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
  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 metho开发者_StackOverflow中文版d 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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消