cssasp.net-mvcvalidationasp.net-mvc-5foolproof-validation

RequiredIf Not Firing Client Side Validation Styling


In my Model I have this:

[RequiredIf("Operation", 2)]
public string Test_Type { get; set; }

I am using MVC Fool Proof Validation.

In my view:

<div class="col-md-6 form-group">
    <label class="col-md-4 control-label">Operation Type: <span class="fa fa-asterisk text-danger"></span></label>
    <div class="col-md-8">
        @Html.DropDownListFor(model => model.Operation, (IEnumerable<SelectListItem>)ViewBag.OperationChoice, "-- Select Operation --", new { @class = "form-control" })
        <br />
        @Html.ValidationMessageFor(model => model.Operation, "", new { @class = "text-danger" })
    </div>
</div>

<div class="row" id="TestType-div">
    <div class="col-md-6 form-group">
        <label class="col-md-4 control-label">Type Of Test: <span class="fa fa-asterisk text-danger"></span></label>
        <div class="col-md-8">
            @Html.DropDownListFor(model => model.Test_Type, (IEnumerable<SelectListItem>)ViewBag.Test_TypeChoice, new {@class = "form-control"})
            <br/>
            @Html.ValidationMessageFor(model => model.Test_Type, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

This validation is working, but it does not highlight the Test_Type dropdownlist in a red outline with the css class input-validation-error. How do I use the RequiredIf annotation in a way that when the form is invalid, it highlights the field with the input-validation-error class name?

Check This Code Online Codepen.io


Solution

  • To enable client validation, include MvcFoolproofValidation.js with the standard client validation script files:

    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
    <script src="../../Scripts/MvcFoolproofValidation.js" type="text/javascript"></script>
    

    jQuery Validation If you are using jQuery validation, include MvcFoolproofJQueryValidation.js with the standard client validation script files:

    <script src="../../Scripts/jquery.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-validate.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
    <script src="../../Scripts/MvcFoolproofJQueryValidation.js" type="text/javascript"></script>
    

    MVC 3 jQuery Unobtrusive Support

    <script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>
    

    If still client side validation not showing check that your drop-down has input-validation-error class. if class is present and not showing red border then add style :

    .input-validation-error
    {
        border: 1px solid #CD0A0A;
    }