简体   繁体   English

在MVP模式上显示验证错误的最佳实践

[英]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... 我有一个用于用户注册的Presenter,我需要在其上实现验证错误,我要做的是为每个验证错误创建了一个方法,例如UserCannotBeEmpty() InvalidEmailAddress()UserAlreadyExists()UserAlreadyExists()等。 ...

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. 如果要检查20个不同的字段,并且每个字段都需要不同的验证类型, 则必须实现那么多种方法。

Sure, there are some common ways to help you here. 当然,这里有一些常见的方法可以为您提供帮助。 You probably don't need UserCannotBeEmpty and PasswordCannotBeEmpty methods. 您可能不需要UserCannotBeEmptyPasswordCannotBeEmpty方法。 Most likely you'll be fine with CannotBeEmpty(string errorMessage) . 最有可能的是,您可以使用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. 请记住,应在模型侧验证特定于域的验证(例如,您的业务规则,例如“发货日期不能早于生产日期”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM