Client side validation?

Topics: Writing modules
Feb 21, 2012 at 1:02 AM

Hi all, what are my options for implementing client side validation with orchard - and will Orchard 1.4 handle it better? Thanks for any suggestions, Dyr

Feb 21, 2012 at 1:53 AM

I investigated this lastnight & here's what worked for me:

  1. Add the class described in the last post of this thread: to your module
  2. Add jquery.validate.min.js and jquery.validate.unobtrusive.min.js to your scripts folder (it's not included in orchard)
  3. Then activate client side validation in your views like so:

@model LETS.Models.MemberPart
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true; 
    @Html.LabelFor(model => model.FirstName, T("First name"))
    @Html.EditorFor(model => model.FirstName)
    @Html.ValidationMessageFor(model => model.FirstName)
    @Html.LabelFor(model => model.LastName, T("Last name"))
    @Html.EditorFor(model => model.LastName)
    @Html.ValidationMessageFor(model => model.LastName)
There's an issue to vote for here:

Feb 21, 2012 at 3:50 AM

Thanks for the reply! Admittedly, I did stumble across that thread – though steered clear due to Autofac; why, because I wasn’t sure how to implement it? Therefore, no time like the present to learn – do I have to edit global/web.config files, name this module something specific and place it in a specific folder [e.g. Autofac] for instance? Any guidance you can offer will be greatly appreciated, cheers Dyr.

Feb 21, 2012 at 4:40 AM

You're not alone, it's new to me too.  But the class just worked, I added it to the root folder of my module (had to reference Autofac).    Mind you I did comment/refer back to the discussion thread for my own reference as it's described as a hack.  How would it affect localization could be a question.  I assume you're building a module?

Above, I missed out adding the scripts to ResourceManifest.cs

       public void BuildManifests(ResourceManifestBuilder builder)
            var manifest = builder.Add();

Not necessary to edit any web.config, the HtmlHelper lines in the view do the job.

Feb 21, 2012 at 10:21 AM
Edited Feb 21, 2012 at 6:43 PM

Yeah mate, I don't like the terms 'dirty' and 'hack' being used in the same thread as well, but as you know - it works.  Hopefully, 1.4+ is a little more friendly - so for now - I'm grateful for your guidance and 'rodpl's method. Thanks again, Cheers Dyr