Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 client validation not working

I have a required annotation on my model:

[Required(ErrorMessage = "Please choose an option")]
public bool? AnyDebts { get; set; }

I have enabled client validation in the web.config:

   <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

I have referenced the jquery scripts in my layout:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.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/jquery-ui-1.8.6.custom.min.js")" type="text/javascript"></script>

What else do I need to do to make client validation work? Server side validation is still working.

EDIT:

Ah ha!

I have found that client side validation is working.

However, specifically, I have found that model properties are not being validated client side are those annotated with custom attributes. For example:

[BooleanRequiredToBeTrue(ErrorMessage = "You must agree to the statements listed")]
public bool StatementAgree { get; set; }

The code for the attribute:

public class BooleanRequiredToBeTrueAttribute: RequiredAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && (bool)value;
    }
}

Are these not validated client side anymore?

like image 508
MightyAtom Avatar asked Jan 26 '11 09:01

MightyAtom


2 Answers

<script src="@Url.Content("~/Scripts/jquery-1.4.4.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>

are the only required scripts for client validation to work.

And as always here's a full working demo:

Model:

public class MyViewModel
{
    [Required(ErrorMessage = "Please choose an option")]
    public bool? AnyDebts { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

View:

@model AppName.Models.MyViewModel
@{
    ViewBag.Title = "Home Page";
}
<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>
@using (Html.BeginForm())
{
    <div>Debts</div>
    <span>Yes</span> @Html.RadioButtonFor(x => x.AnyDebts, true)
    <span>No</span> @Html.RadioButtonFor(x => x.AnyDebts, false)

    @Html.ValidationMessageFor(x => x.AnyDebts)
    <input type="submit" value="OK" />
}

Remark: I haven't included jquery-1.4.4.js in my view because it is already referenced in the layout.

like image 83
Darin Dimitrov Avatar answered Sep 26 '22 15:09

Darin Dimitrov


Take a look on this code.

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class RequiredIfAttribute : RequiredAttribute
{
    public string PropertyName { get; set; }
    public Enums.BasicOperationType BasicOperationType { get; set; }

    public RequiredIfAttribute(string propertyName, Enums.BasicOperationType BasicOperationType)
    {
        this.PropertyName = propertyName;
        this.BasicOperationType = BasicOperationType;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        Type objectType = validationContext.ObjectType;
        PropertyInfo[] neededProperties = objectType.GetProperties().Where(propertyInfo => propertyInfo.Name == PropertyName).ToArray();
        if (Core.Helpers.EnumHelper.TryParse<Enums.BasicOperationType>(Convert.ToInt16(neededProperties[0].GetValue(validationContext.ObjectInstance, null))) == BasicOperationType)
            return base.IsValid(value, validationContext);

        return base.IsValid(0, validationContext);
    }
}
like image 25
Igor Franco Brum Avatar answered Sep 23 '22 15:09

Igor Franco Brum