Problems with Data and Globalize

1

When submitting a form with a dd / MM / yyyy date, the value is converted to the US format in the controller, leaving MM / dd / yyyy. Through the response question , I configured globalize to solve this problem. However, the error persists.

The following bundle is configured:

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.globalize.js",
                    "~/Scripts/jquery.validate.unobtrusive.min.js",
                    "~/Scripts/jquery.unobtrusive-ajax.js"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/globalize").Include(
                  "~/Scripts/globalize.js"));

Code to load the bundles

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/globalize")
@Scripts.Render("~/bundles/jqueryval")

Configuration line in section System.Web in Web.Config

<globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />

Datetime field in cshtml

@Html.TextBoxFor(model => model.Inicio, "{0:dd/MM/yyyy}", new { @class = "form-control" })
    
asked by anonymous 14.05.2015 / 14:43

3 answers

1

I think my answer you've pointed out is outdated. This one should solve your problem more specifically . For the specific case of jQuery Validate and Globalize, you need to have a specific ordering of the scripts.

Avoid separating the bundle from Globalize from the bundle of jQuery Validate. Also avoid using @Html.EditorFor() for date fields. Use @Html.TextBoxFor() instead.

EDIT

Usage of @Html.TextBoxFor() for dates case:

@Html.TextBoxFor(model => model.MinhaData, "{0:dd/MM/yyyy}", new { @class = "minha_classe_de_css"})
    
14.05.2015 / 17:34
0

I think this error is related to the culture set up in jQuery validation.

Try to add the Scripts/cultures/Globalize.culture.pt-BR.js script to the bundle and then add that script to override jQuery date validation:

<script type="text/javascript">
    $(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("pt-BR");
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });
</script>

Information found here .

    
14.05.2015 / 15:09
0

Vinicius knows it's complicated, I did a little tutorial here:

link

When you run the command Install-Package jquery.Validation.Globalize , it does not come complete package. You should do this manually.

On the globalize site documented:

  

We do NOT embed any i18n data within our library. However, we make it   really easy to use. Read How to get and load CLDR JSON data for more   information on its usage.

    
09.08.2017 / 18:56