Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC2 DataAnnotations with Server-side validation

How do you validate an entity containing DataAnnotations without using the MVC library? Using Model.IsValid is fine when you're within the Presentation layer, but what about when you want to ensure the model is valid in the Domain/Business layer? Do I need a separate validation framework, or is there an easy way I'm missing?

Thanks for any help,

Mark

like image 829
user311131 Avatar asked Jul 09 '26 10:07

user311131


1 Answers

I suppose you mean ModelState.IsValid by Model.IsValid, right? Well, DataAnnotions don't depend on MVC at all, so you can always use the IValidatableObject interface.

Or perhaps the Validator class will be more appropriate, by using Validator.ValidateObject(object, ValidationContext).

like image 158
rdumont Avatar answered Jul 11 '26 12:07

rdumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!