Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Client-side validation for non-editor form (and not using XForms)?

Vote:
 

Anyone know if there is a recommended way to do client-side validation for a form presented to a non-edit-mode user?  Also not using XForms...

I've been trying to get the classic MVC unobtrusive validation to work but the HTML coming from Html.EditorFor(m => m.FieldName) does not spit out the correct annotations needed by those libraries (data-val, data-val-required, etc).  I assume Episerver has removed those so that it doesn't interfere with Editor-mode validation.  Does anyone know how to turn that back on without having to write out the attributes manually?

Using the latest version of Episerver 7.1.  Here's an example of my view & controller:

Controller:

[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }

View:

@using (Html.BeginForm()) 
{
    <div>@Html.ValidationSummary(true)</div>
    <div>            
        @Html.LabelFor(m => m.Name)  
        @Html.ValidationMessageFor(m => m.Name)  
        @Html.EditorFor(m => m.Name)
    </div>
    <input type="submit" value="Submit" class="btn"/>
}

    

Many thanks,

Ryan

#75633
Oct 02, 2013 0:10
Vote:
 

Think I just answered my own question.  In web.config under appSettings add:

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

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

Seems to work!  Hopefully won't break Edit mode.

#75634
Oct 02, 2013 0:39
Vote:
 

If you are worried that your settings in web.config might interfere with EPiServer UI, you could remove these settings on specific paths:

For example

<location path="EPiServer">
 <appSettings>
  <remove key="ClientValidationEnabled" />
 </appSettings>

</location>

#75656
Oct 02, 2013 14:05
Vote:
 

Thank you!  This is really helpful.

#75657
Oct 02, 2013 15:44
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.