Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Client Validation works intermittently
    primarykey
    data
    text
    <p><em>update: if you set at least one breakpoint in javascript, validation start to works, but without it does not work</em></p> <p><em>update: adding jquery tag as this may be connected to validation plugin</em></p> <p>I have MVC 3 version, <code>System.Web.Mvc</code> product version is: <code>3.0.20105.0</code> modified on <code>5th of Jan 2011</code> - i think that's the latest.</p> <p>I’ve notice that client validation is not working as it suppose in the application that we are creating, so I’ve made a quick test. </p> <p>I’ve created basic MVC 3 Application using <em>Internet Application</em> template.</p> <p><strong>I’ve added Test Controller:</strong></p> <pre><code>using System.Web.Mvc; using MvcApplication3.Models; namespace MvcApplication3.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } public ActionResult Create() { Sample model = new Sample(); return View(model); } [HttpPost] public ActionResult Create(Sample model) { if(!ModelState.IsValid) { return View(); } return RedirectToAction("Display"); } public ActionResult Display() { Sample model = new Sample(); model.Age = 10; model.CardNumber = "1324234"; model.Email = "somedata@test.com"; model.Title = "hahah"; return View(model); } } } </code></pre> <p><strong>Model:</strong></p> <pre><code>using System.ComponentModel.DataAnnotations; namespace MvcApplication3.Models { public class Sample { [Required] public string Title { get; set; } [Required] public string Email { get; set; } [Required] [Range(4, 120, ErrorMessage = "Oi! Common!")] public short Age { get; set; } [Required] public string CardNumber { get; set; } } } </code></pre> <p>And 3 views:</p> <p><strong>Create:</strong></p> <pre><code>@model MvcApplication3.Models.Sample @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Create&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @*@{ Html.EnableClientValidation(); }*@ @using (Html.BeginForm()) { @Html.ValidationSummary(false) &lt;fieldset&gt; &lt;legend&gt;Sample&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Title) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Title) @Html.ValidationMessageFor(model =&gt; model.Title) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Email) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Email) @Html.ValidationMessageFor(model =&gt; model.Email) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Age) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Age) @Html.ValidationMessageFor(model =&gt; model.Age) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.CardNumber) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.CardNumber) @Html.ValidationMessageFor(model =&gt; model.CardNumber) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; @*&lt;fieldset&gt; @Html.EditorForModel() &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; *@ } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p><strong>Display:</strong></p> <pre><code>@model MvcApplication3.Models.Sample @{ ViewBag.Title = "Display"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Display&lt;/h2&gt; &lt;fieldset&gt; &lt;legend&gt;Sample&lt;/legend&gt; &lt;div class="display-label"&gt;Title&lt;/div&gt; &lt;div class="display-field"&gt;@Model.Title&lt;/div&gt; &lt;div class="display-label"&gt;Email&lt;/div&gt; &lt;div class="display-field"&gt;@Model.Email&lt;/div&gt; &lt;div class="display-label"&gt;Age&lt;/div&gt; &lt;div class="display-field"&gt;@Model.Age&lt;/div&gt; &lt;div class="display-label"&gt;CardNumber&lt;/div&gt; &lt;div class="display-field"&gt;@Model.CardNumber&lt;/div&gt; &lt;/fieldset&gt; &lt;p&gt; @Html.ActionLink("Back to List", "Index") &lt;/p&gt; </code></pre> <p><strong>Index:</strong></p> <pre><code>@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create", "Create") &lt;/p&gt; &lt;p&gt; @Html.ActionLink("Display", "Display") &lt;/p&gt; </code></pre> <p>Everything is default here – <em>Create Controller</em>, <em>AddView from controller action</em> with model specified with proper scaffold template and using provided layout in sample application.</p> <p>When I will go to <em>/Test/Create</em> client validation in most cases works only for <code>Title</code> and <code>Age</code> fields, after clicking <em>Create</em> it works for all fields (create does not goes to server).</p> <p>However in some cases (after a build) <code>Title</code> validation is not working and <code>Email</code> is, or <code>CardNumber</code> or <code>Title</code> and <code>CardNumber</code> but <code>Email</code> is not. But never all validation is working before clicking <em>Create</em>.</p> <p>I’ve tried creating form with <code>Html.EditorForModel</code> as well as enforce client validation just before BeginForm:</p> <pre><code>@{ Html.EnableClientValidation(); } </code></pre> <p>I’m <a href="http://dl.dropbox.com/u/6644278/MvcValidationSample.zip" rel="nofollow">providing a source code for this sample on dropbox</a> – as maybe our dev env is broken :/ I’ve done tests on IE 8 and Chrome 10 beta.</p> <p>Just in case, in web config validation scripts are enabled:</p> <pre><code>&lt;appSettings&gt; &lt;add key="ClientValidationEnabled" value="true"/&gt; &lt;add key="UnobtrusiveJavaScriptEnabled" value="true"/&gt; &lt;/appSettings&gt; </code></pre> <p><strong>So my questions are</strong></p> <p>Is there a way to ensure that Client validation will work as it supposed to work and not intermittently?</p> <p>Is this a desired behavior and I'm missing something in configuration/implementation?</p> <p><em>update: if you set at least one breakpoint in javascript, validation start to works, but without it does not work</em></p> <p><em>update: adding jquery tag as this may be connected to validation plugin</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload