30

Resolved

Client validation doesn't work on custom controller in module

description

Changeset #5459 "#17754: Localizing validation attributes" caused client
validation fail. The problem is that unobtrusive html data attributes
are no longer rendered in html since Orchard 1.3.

Added lines in OrchardStarter.cs in 5459
// Register localized data annotations
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());
caused this. Here is the test.

ValidationController.cs
namespace Validation.Controllers
{
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    using Orchard.Themes;

    [Themed]
    public class ValidationController : Controller
    {
        public ActionResult Index()
        {
            return View(new ValidationEditModel());
        }
    }

    public class ValidationEditModel
    {
        [Required]
        public string Name { get; set; }
    }
}
Index.cshtml
@using Validation.Controllers
@model ValidationEditModel
@{
  HtmlHelper.ClientValidationEnabled = true;
  HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
@using (Html.BeginForm())
{
  @Html.EditorForModel()
}
Html rendered in Orchard 1.2

<input class="text-box single-line" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true">


Html rendered in Orchard 1.3

<input class="text-box single-line" id="Name" name="Name" type="text" value="" />

file attachments

comments

rodpl wrote Dec 4, 2011 at 4:53 PM

I've attached sample module with client validation. This sample works with 1.2 but doesn't with 1.3.

rodpl wrote Dec 4, 2011 at 8:01 PM

Small dirty workaround is described here

http://orchard.codeplex.com/discussions/278362

randompete wrote Dec 5, 2011 at 4:15 PM

Note: revision numbers aren't a good way to identify changesets. Your local repository might have different revision numbers to other people's (for instance if anyone makes commits on their local repository, works on a fork, etc.) Instead you want the changeset hash which is the proper identifier: 5a20645193e1

rodpl wrote Dec 5, 2011 at 6:55 PM

Yes .You are right it is changeset 5a20645193e1. My bad

sfmskywalker wrote Aug 20, 2013 at 5:44 AM

I tried kmulder's solution (https://orchard.codeplex.com/discussions/278362) and it works like a charm.
I'll ask him to do a pull request and apply the patch. Given the number of votes, I propose we fix this for 1.7.1. I myself came across the need for client side validation a couple of times now.

morrisonbrett wrote Aug 23, 2013 at 1:50 AM

I just tried kmulder's solution and it works. Let's get this patched.

sfmskywalker wrote Aug 24, 2013 at 5:21 AM

Fixed in changeset 7f463d07cc1f5e18ae047d5019961734c5d71b8e