12
I’m having problems with the date fields of my application.
Follow this answer that seemed more appropriate, Datetime field error message, I configured my ASP.NET MVC application as follows:
- I installed the jquery-globalize package via
Install-Package jquery-globalize
; I installed the package jquery.validation.globalize via
Install-Package jquery.validation.globalize
;I added the tab to Web.config reference to the language en.
<system.web> ... <globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" /> ... </system.web>
Although in the answer indicates to add in
<configuration>
, the application was error in Web.config file and then I got in tag<system.web>
.
I set up my Bundleconfig.Cs:
I organized my scripts according to the example of the answer, leaving so:
The script with
Globalize.culture("pt-BR");
at the end of the picture was a last attempt I made.
jquery.validation.js loads through other views, when needed, via@section footerSection{ Scripts.Render("~/js/jqueryval"); }
. So, my output of the scripts is like this, in debug mode:This is the properties of my View Model used to render the fields:
[DataType(DataType.Date)] [DisplayName("Data Vencto.")] [Required(ErrorMessage = "Informe o campo {0}")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public DateTime? txtDataVencimento { get; set; } [DataType(DataType.Date)] [DisplayName("Data Pagto.")] [Required(ErrorMessage = "Informe o campo {0}")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public DateTime? txtDataPagamento { get; set; }
I’ve tested with
DataFormatString = "{0:yyyy-MM-dd}"
, but it didn’t work either.Razor:
<div class="form-group"> @Html.LabelFor(model => model.txtDataVencimento, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.txtDataVencimento, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.txtDataVencimento, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.txtDataPagamento, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.txtDataPagamento, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.txtDataPagamento, "", new { @class = "text-danger" }) </div> </div>
So I ask you for help: What may still be missing or wrong?
EDITION
According to the response of Gypsy Morrison, the order of my scripts should be another and so I applied the Bundle as given guidance.
var bundle = new ScriptBundle("~/js/jqueryval") { Orderer = new AsIsBundleOrderer() };
bundle
.Include("~/Static/js/jquery.validate.js")
.Include("~/Static/js/jquery.validate.unobstrusive.js")
.Include("~/Static/js/globalize/globalize.js")
.Include("~/Static/js/jquery.validate.globalize.js");
bundles.Add(bundle);
With that my scripts were in the order indicated:
However, still the error continues, but demonstrating message in English: "Please enter a Valid date."
Which was bold in black.
EDITION
When editing my Bugles to look like this:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/js/jquery").Include(
"~/Static/js/jquery-{version}.js",
"~/Static/js/jquery.plugin.js"));
bundles.Add(new ScriptBundle("~/js/jqueryval").Include(
"~/Static/js/jquery.validate.js",
"~/Static/js/jquery.validate.unobtrusive.js",
"~/Static/js/globalize/globalize.js",
"~/Static/js/jquery.validate.globalize.js"));
bundles.Add(new ScriptBundle("~/js/bootstrap").Include(
"~/Static/js/bootstrap.js"));
bundles.Add(new StyleBundle("~/Static/css/styles").Include(
"~/Static/css/bootstrap.css",
"~/Static/css/site.css"));
}
And with the standard ordering done by Bundleconfig, my scripts stayed in that order:
The date validation passed, but the values went wrong:
Hello, you forgot to implement CLDR file as the globalize depends on it. More information: http://cldr.unicode.org/, I made a small tutorial here : https://answall.com/a/227756/54019
– Matheus Miranda