开发者

Best practice to show validation errors on MVP pattern

I have a Presenter that is used for user registration and I need to implement validation errors on it, what I've done is I created a method for each validation error, like UserCannotBeEmpty(), InvalidEmailAddress(), UserAlreadyExists() and so on...

It is ok for just a few fields, but it just doesn't look good if I have a lot of fields, 开发者_开发知识库I would have to create a lot of methods for that...

What't the best way to do this?

thanks!


What's wrong with more fields - more validation? If you have 20 different fields to check, and each one requires different type of validation, you'll have to implement that many methods.

Sure, there are some common ways to help you here. You probably don't need UserCannotBeEmpty and PasswordCannotBeEmpty methods. Most likely you'll be fine with CannotBeEmpty(string errorMessage). Refactor out stuff that's common (null checks, alphanumeric checks, datetime/number parsing and so forth - type dependant), and implement field-specific validation only when needed.

Keep in mind, domain specific validation (your business rules, eg. "shipment date cannot occur before production date") should be validated on model-side.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜