6

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?

1
  • why are you calling jquery twice? Commented Nov 3, 2012 at 19:10

3 Answers 3

6
<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.

Sign up to request clarification or add additional context in comments.

1 Comment

Any ideas on this one @Darin Dimitrov? stackoverflow.com/questions/5663563/…
1

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);
    }
}

Comments

0

For my case, it only happens when using IE in quirks mode, my settings are Browser Mode: IE9, Document Mode: Quirks. This results in client side validation not working for me. However, the same page works well in Firefox,Chrome, and IE9, Browser Mode: IE9, Document Mode: IE9 standards when using the developer toolbar. Not that its a solution, but a possible lead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.