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?
<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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With