0

My validation requirements for a the fields in a form are contained in an external table so that they can be updated without altering and rebuilding the code.

I have approximatley 100 fields with a mixture of validation requirements - range, required, regular expression and dependency on other fields. One example is date range validation. A date of birth field requires a date range which is between -10 years and -50 years of the current date.

I have read around the subject but have not identified a pattern for the complete solution.

I am using Visual Studio 2010 with MVC 3 and Entity Framework.

Any help with this would be gratefully received. Thanks in advance.

4
  • If you are looking for a client-side validation solution using jQuery, I might be able to help you - however, I will have to ask my boss as I wrote the validator plugin at work, which means its his property. Commented Sep 13, 2012 at 12:17
  • Hi Jeff - thanks for the offer however the jQuery is OK. What I'm really after is the MVC parts for creating dynamic validation attributes and then using this as unobtrusive jQuery/JavaScript. Commented Sep 13, 2012 at 12:39
  • What are you validating? String values? Commented Sep 13, 2012 at 12:44
  • Thanks. See list above - esentially everything as I'm trying to build a table driven general validation model. Cheers Peter Commented Sep 13, 2012 at 13:39

1 Answer 1

1

In a simple level I think you can still use the built-in Data-Annotations validation attributes to does the validation and for that you should map the validation rules stored in the table to the attributes.

I think all you have to do is create a custom model validation provider by inheriting the class ModelValidatorProvider. This class contains a single method called GetValidators that returns the collection validators for that model.

You have to implement the GetValidators method and in there you have to make a database call to get the validation rules for the model from the database (or from cache?) and convert them into ModelValidators. You could still use the built-in DataAnnotationsModelValidator to do the validations.

I would suggest you to look into the source code of DataAnnotationsModelValidatorProvider that will give you all the information. In that class what they are doing is basically iterating all the validation attributes applied to the model properties and converting them into ModelValidators through adapters and factories. In your case instead of attributes they are stored as records in tables and I don't think much work will be there.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Mark - a very useful overview.
Thanks Mark, following your sugestions I found the following (see next comment)
(continued) link which describes exactly what to do.
I wrote a blog post on this subject prideparrot.com/blog/archive/2012/9/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.