search

Home  >  Q&A  >  body text

In MVC, should validation be placed in the Controller or the Model?

I am a front-end person and I just started writing about the back-end. This question is quite confusing. Is it better to put it in the Model and return different status codes through the Model for calls by different controllers? Let’s share our experiences. Attached are two related links:

怪我咯怪我咯2807 days ago519

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 17:08:36

    I once had similar doubts. In fact, everyone also said that a thin controller is a fat model, but why does a thin controller have to be a fat model? What is the design principle? The simpler, the better. So doesn’t a fat model violate us? design principles?

    So for this problem, my solution is, since we want to lose weight, then everyone should lose weight, thin controller + thin model verification layer (I call it model_service) + thin model, in this case, even if multiple controllers are reused The verification layer is not a problem, and the model is still a basic addition, deletion, modification and check, and this model will be more flexible. For example, a common problem is that one model refers to the addition, deletion, modification and check of another model, and it can also be reused directly

    In fact, layering is an idea. It does not mean that layering must follow a certain established plan. In that case, it is better not to layer. Since you have adopted the idea of ​​layering, it doesn’t matter whether it is mvc or smvc. Which one is definitely better depends on how you think about how your program is layered. Maybe you have come up with a xxxmvc layered design for convenience. Layering layering, remember that there must be design considerations that can add layers.

    I remember there seems to be a popular saying on the Internet "Any problem in the field of computer science can be solved by adding an indirect middle layer", so you will see many similar concepts: proxy, cache, cgi, factory mode, etc. There is even a concept called "middle layer", so when you think that it is good to put it in any of the two related layers, you can think about whether it would be better to have a middle layer?

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 17:08:36

    According to my rails experience, it is better to write it in the model

    There is a saying about fat model and thin controller in rails
    Rails example:

    Person model validates first_name, last_name fields

    class Person
      include ActiveModel::Validations
    
      attr_accessor :first_name, :last_name
    
      validates_each :first_name, :last_name do |record, attr, value|
        record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
      end
    end
    

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 17:08:36

    Please refer to the JSR-303 Bean Validation API. In addition, the hibernate-validation jar library also extends the validation standard, which is also written in the model and uses annotation.

    reply
    0
  • Cancelreply