0

I have an MVC 3 appliation which I have many integer fields on a form. They all require range validation but the ranges exists in a table in my database. So I would like to create a reusable remote validation tool which will look up the min and max value and return the validation to the view.

I am updating this with some example code of what I would like to do to see my request might clarify what I am looking for:

In my validation class:

        [Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
    public object UW1_Web_Tension_SP { get; set; }

    [Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
    public object UW2_Web_Tension_SP { get; set; }

    [Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
    public object UW3_Web_Tension_SP { get; set; }

In my ValidationController I tried to create a function with multiple parameters but I dont think I can - however I think it shows what I am trying to do more clearly:

        public class ValidationController : Controller 
    {
        public JsonResult CheckIntegerRange(int integer, string EntityName, string AttributeName)
        {
            var result = false;
            int MinInteger = 0;
            int MaxInteger = 100;

            //declare recipe entities
            var context = new MadicoRecipeEntities();

            //set sql statements and get description, etc from attributes view
            var esqlIntegerAttributeDetails = "SELECT VALUE c FROM MadicoRecipeEntities.v_AttributeIntegerRangeDetails AS c " +
                "WHERE c.EntityName = '" + EntityName + "' AND c.Attribute = '" + AttributeName + "'";
            var queryAttributeDetails = context.CreateQuery<v_AttributeIntegerRangeDetails>(esqlIntegerAttributeDetails);
            var RecipeAttributes = queryAttributeDetails.ToList();

            foreach (var AttributeDetails in RecipeAttributes)
            {
                MinInteger = AttributeDetails.Min;
                MaxInteger = AttributeDetails.Max;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }

I have found the following post on the asp.net forums which handled my first obstacle - passing different named attributes to the same validator. However, in this example the name is passed in generically - I need to have the name in order to query the table to get the applicable min and max ranges.

http://forums.asp.net/t/1625928.aspx/3/10

Can anyone point me in the right direction?

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
user988148
  • 21
  • 3

1 Answers1

0

It is called remote validation! Here is an example: remote validation

Ryand.Johnson
  • 1,906
  • 2
  • 16
  • 22
  • Thanks but I've seen these examples. I'm looking for something which will be reusable for more than one field. I have 50 fields in my form which all require the same lookup but I dont want to write 50 json queryies. – user988148 Nov 18 '11 at 23:19
  • 1
    Use the same public JSON validation method, but pass in a type. Then have a private lookup method that did a switch statement on the type. The lookup method would return query needed for that type. – Ryand.Johnson Nov 21 '11 at 16:16
  • This will help you: http://stackoverflow.com/a/10264019/1557032 Specifically, see the REMOTE rule being added. – Ramanpreet Singh Dec 12 '13 at 12:45