Asp.Net MVC 2 Client validation implementation for Enterprise Library Validation Block

拜拜、爱过 提交于 2019-12-22 17:46:07

问题


I've found a very good article about how to use EntLib Validation Block for server validation in MVC 2. But as there pointed out

The current design of EntLib’s Validation Application Block uses the Composite pattern; that is, when we ask for validation for an object, it returns back a single validator object that contains a list of all the validation work to be done. While this is very convenient from a normal usage scenario, the unfortunate side-effect is that we can’t “peek inside” to see what the individual validations are that it’s doing, and therefore can’t generate the appropriate client-side validation hints.

So how is it possible to implement client side validation for EntLib? Is there work around?


回答1:


So I could not find anything about this, so I desided to implement it on my own an publish it on codeplex http://elvalweb.codeplex.com/




回答2:


Alexey, :-) i have my own implementation for validation task that is not related with EntLib, but very similar by concept. For developer it's look like the following:

   ValidationFactory.AddRule<IPerson>(
     x => string.IsNullOrEmpty(x.FirstName) &&
          string.IsNullOrEmpty(x.LastName),
     "Person should have a name", "validation set 1");
   IPerson p = UnityHelper.DefaultContainer.Resolve<IPerson>();
   ValidationResults res = ValidationFactory.Validate<IPerson>(p,"validation set 1");
   if(!res.IsValid)
   {
     foreach (ValidationResult vr in res)
     {
       var msg= vr.Message;
       var validated_instance = vr.Target;
       var Validator_instance = vr.Validator;
     }
   }   

If you are interesting in it let me know, i will cut it from my current project into separate solution.



来源:https://stackoverflow.com/questions/2666954/asp-net-mvc-2-client-validation-implementation-for-enterprise-library-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!