In MVP where to write validations
Asked Answered
D

3

34

In Model-View-Presenter pattern where should we write validations of user input.

Disembogue answered 20/10, 2008 at 8:8 Comment(0)
D
21

Domain specific rules/validations should be in the Model. You can have a model.validate() to let you know if the rules are not violated. Look at Rails model (ActiveRecord) classes for a good implementation of this concept.

The View should make it difficult for the user to key in invalid input. So 'entering a string for a numeric value' class of input errors should be nipped before reaching the presenter. There may be some duplication of validations between model and view. E.g. AttributeX must range between 1-100. This must be validated in the model.. at the same time you may want to slot in a spinner in the UI with the minValue and maxValue range set to 1-100.

Doxology answered 20/10, 2008 at 8:42 Comment(1)
Could you please answer #8852433 ?Oratory
O
4

I usually keep my view completely clean, no logic there. But I don't do a lot of web development. In Ajax-ish situations you might want to have client side validation that has to go in the view.

Business logic validation goes in the model. With business logic validation I mean things like checking minimum order size etc.

Input validation goes in the presenter. This can be things like checking if a number field doesn't contain non numeric characters. But depending on your situation this can also mean checking if files exist etc.

In more complex cases where validation should be reusable in different places I usually separate it into a validation engine that can be called in different places. This solves some problems with duplicating validation code that is used in the presentation layer as well as the persistence layer for example.

Orest answered 20/10, 2008 at 8:43 Comment(1)
That's the approach we've taken - we have our validation in a shared package so we can check the input twice: once in the client so we can inform the user right away if invalid input, and once again in the server to make sure the user isn't cheating.Messiah
S
0

Presenter....

The view should have have "widgets" that prevent invalid input where possible.

Shipworm answered 20/10, 2008 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.