17

I have the view below:

    @Html.LabelFor(m => m.CompanyPostCode)
    @Html.TextBoxFor(m => m.CompanyPostCode)

    @Html.LabelFor(m => m.CompanyCity)
    @Html.TextBoxFor(m => m.CompanyCity)

    @Html.HiddenFor(m => m.CompanyCityID)

All attributes are marked as [Required] in my view model. Then problem is that my CompanyCityID (marked as Required) is hidden and thus no validation is done in the view. If I show this attribute in my view the validation is done.

enter image description here

My question: is it possible to perform a validation on a hidden field? A workaround exist?

It may seems a little strange to validate a hidden field. The reason is that this field is filled from jQuery based on special rules. If it is not filled, I know something is not valid on the view.

Thanks.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Bronzato
  • 9,438
  • 29
  • 120
  • 212

4 Answers4

29

The possible reason can be that there is ignore: ':hidden' line in jquery.validate.unobtrusive.js file.

After 1.9.0 version it is a default behaviour. You can fix that manually by adding

$.validator.setDefaults({ ignore: [] });

As you can see here

Another change should make the setup of forms with hidden elements easier, these are now ignored by default (option “ignore” has “:hidden” now as default). In theory, this could break an existing setup. In the unlikely case that it actually does, you can fix it by setting the ignore-option to “[]” (square brackets without the quotes).

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • I really hate being that guy that corrects people but the ignore setting isn't set by jquery.validate.unobtrusive.js. Also, be aware of how setDefaults works before using it. If you set defaults after you've already wired up the validation for the form then you're too late with this call. Please see my write up for a more in-depth explanation. http://stackoverflow.com/questions/8466643/jquery-validate-enable-validation-for-hidden-fields/14574974#14574974 – JustinMichaels Feb 14 '13 at 18:48
  • @JustinMichaels, maybe something has changed since I wrote this answer. At that time I checked jquery.validate.unobtrusive.js and ignore setting was there. – Chuck Norris Feb 15 '13 at 05:59
  • 2
    Excellent answer, I was looking for this for like 3 hours... "Thank YOU, Chuck Norris!" (like from the movie Dodgeball with Vince Vaughn) – user1477388 Apr 29 '13 at 20:20
  • Hey wait you're too young to be Chuck Norris! – MetaGuru Aug 16 '13 at 19:40
  • Chuck Norris doesn't know what is age... He lives forever. – Chuck Norris Aug 17 '13 at 11:57
  • 4
    Keep in mind that you **don't** put `$.validator.setDefaults({ ignore: [] });` in a `$(document).ready` as this will be too late. If you can't do so, use: `$('form').validate().settings.ignore = []` – Luke Jul 16 '15 at 12:00
  • This worked for me `$('form').validate().settings.ignore = ":hidden:not(#SelectedReports)";` while `$('form').validate({ ignore: ":hidden:not(#SelectedReports)"});` did not. Just a tip for anyone else stuck on this. – Eric D. Johnson Apr 04 '16 at 20:38
0

You can also just comment out this line in the jquery.validate.js file. ignore: ":hidden"

user973671
  • 1,620
  • 6
  • 27
  • 39
0

If you don't have to use javascript, in your Controller, and in your action of the related view, you can add a model error before validating your model. Example:

 [HttpPost]
        public ActionResult Fix(YourModel mdl)
    {

    if (mdl.CompanyCityID==0)
        ModelState.AddModelError("", "Your error message!");

    if (ModelState.IsValid)
    {


       //
       //Some code
       //

        return View("YourView", yourlist);
    }

    return View(mdl);
}
nrtn93
  • 101
  • 2
  • 10
-1

You can always put @Html.HiddenFor(m => m.CompanyCityID) inside a hidden div and change it to EditorFor().

CSS

.hidden {
    display: none;
}​

View

<div class="hidden">
        @Html.EditorFor(m => m.CompanyCityID)
</div
Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • 2
    This does not seem to work. I think it can still tell that it's hidden, even if it's a parent that's hidden. – Mike Cole Oct 04 '13 at 19:12