0

I am using MVC Foolproof validation in my application. Scenario is I have a dropdownlist named CustomerType with the the following values

 Id     Name
 1      Student
 2      Non-Employed
 3      Employed
 4      SelfEmployed

and I have one more property in my viewmodel public string CompanyAddress{ get; set; }.My goal is to make CompanyAddress required if dropdownlist has values 3 or 4

I have tried the following

   [Required(ErrorMessage = "Please select status of the customer", AllowEmptyStrings = false)]
    public int? customerTypeID { get; set; }
    public SelectList customerTypeList { get; set; }

   [RequiredIf("IsCompanyAddressRequired", true, ErrorMessage = "please enter company address")]
    public string CompanyAddress { get; set; }


 public bool IsCompanyAddressRequired
    {
        get
        {
            if (customerTypeID == 3 || customerTypeID == 4)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }

The above code is working properly on the server side but on the client side i'm getting the following error

 `Uncaught TypeError: Cannot read property 'value' of undefined`

Validation is being referenced as follows

 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/plugins/jQueryVal/jquery.unobtrusive*",
                    "~/plugins/jQueryVal/jquery.validate*",
                    "~/plugins/foolproofValidation/MvcFoolproofJQueryValidation.min.js",
                    "~/plugins/foolproofValidation/mvcfoolproof.unobtrusive.min.js",
                    "~/plugins/foolproofValidation/MvcFoolproofValidation.min.js"
                    ));
Sparky
  • 98,165
  • 25
  • 199
  • 285
ksg
  • 3,927
  • 7
  • 51
  • 97
  • This cant work unless you generate an input for property `IsCompanyAddressRequired` in the view (and then you would need to dynamically update it based on the selected value in the dropdownlist. Insread, use a `[GreaterThan("customerTypeID", 2)]` and delete the `bool` property –  Nov 25 '15 at 07:50
  • Thanks for the reply..Here i am getting the error `Foolproof.GreaterThanAttribute` does not contain a constructor that takes 2 arguments. – ksg Nov 25 '15 at 08:42
  • Sorry, getting confused with my own validation attributes :) - Let me check the docs (not even sure foolproof supports this) –  Nov 25 '15 at 08:47
  • Looks like it needs to be `[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]` –  Nov 25 '15 at 08:52
  • Thanks mate its working and please post the answer..:) – ksg Nov 25 '15 at 09:03

1 Answers1

7

Your validation attribute could not work on the client on the client unless you were to generate a input (say) <input name="IsCompanyAddressRequired" value="false" /> (or value="true" depending on the initial value of customerTypeID) and then dynamically update the value attribute using javascript when the dropdownlist value is changed.

Instead use

[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]
public string CompanyAddress { get; set; }

and delete your IsCompanyAddressRequired property.