MVC3/4 Validating Hidden Fields
Asked Answered
M

2

2

I have set up model validation for my form but validation doesn't seem to work at all. I don't suppose anybody can help. I've tried using the below work-around but that keeps pulling up an 'undefined' error in firebug.

Example Work-Around Script:

<script type="text/javascript">
    $(document).ready(function () {            
        $.validator.setDefaults({ ignore: [] });
    });
</script>

Example Text Field (Note: autocomplete text field with name of customer):

    @Html.TextBox("txtCustomer", null, new { @id = "txtCustomer", @class = "txt" })

Example Hidden Field (Note: When selection is made from the autocomplete field, the id is injected into the hidden field):

    @Html.HiddenFor(model => model.Customer_ID, new { @id = "hCustomerID" })

Example Model Data Annotation

    [Range(1, Int32.MaxValue, ErrorMessage = "Please select a customer")]
    public int Customer_ID { get; set; }

EDIT: Screenshot of errors

Sorry i have to post links to the screenshots because my rep points are high enough.

UI Postback Error

EDIT: Screenshot showing page source

Screenshot of Page Source

Magnolia answered 24/1, 2013 at 14:30 Comment(3)
What is it telling you is undefined?Britannic
$.validator.setDefaults({ ignore: [] });Magnolia
possible duplicate of Perform validation on hidden fieldsExecrate
F
9

Its too late to change the ignore on the validator in this way after the unobtrusive script. This is because the validator only pulls in the defaults once - when it is created. The unobtrusive script creates the validator for you. You need to reference the existing validator object and update it.

try this

<script>
    $(document).ready(function () {            
        $('form').validate().settings.ignore = []
    });
</script>
Farnsworth answered 24/1, 2013 at 16:57 Comment(8)
I tried a similar approach with that line and it still shows an error, refer to original post for screenshot.Magnolia
from this error i.sstatic.net/b0XZp.png 'validate is not a function' either you haven't included the jquery validate js file or you have included it not high enough in the page - check this firstFarnsworth
seems in order but I think you need to tighten up this question - you say validation "doesn't seem to work at all", and you have an error 'validate is not a function'... this indicates you have no client side validation working, so is the question about validating hidden fields or troubleshooting your jquery validate setup?Farnsworth
I guess i'm just looking for a bit of help to figure out how to properly validate hidden fields. Been scratching my head for a while as to why it doesn't work quite the same as standard fields. On a plus not if you use: var validator = $("#mvcForm").data('validator'); validator.settings.ignore = "";, this seems to allow the setting to take hold.Magnolia
Issue i'm running into now is that the required message won't disappear even when a value has been injected into the hidden field via jquery ui autocomplete.Magnolia
try revalidating the hidden field manually using element(...) after you have changed the value docs.jquery.com/Plugins/Validation/Validator/element good luckFarnsworth
You are awesome, thank you very much for that, Just got to do a bit of tweaking to get it working using onchange. Wonder why hidden fields don't work normally like the others???Magnolia
I couldnt get this function to work unless it was at the bottom of my document as well, just an FYI if you are placing it at the beginning of the DOM.Funest
S
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.Customer_ID>Int32.MaxValue || mdl.Customer_ID<1)
            ModelState.AddModelError("", "Your error message!");

        if (ModelState.IsValid)
        {


           //
           //Some code
           //

            return View("YourView", yourlist);
        }

        return View(mdl);
    }
Shallow answered 29/3, 2016 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.