我正在尝试使用 cldr-data 和 globalize 功能来验证我在 ASP.NET Core MVC Web 项目中的输入。
出现问题是因为它似乎可以根据navigator.language
属性加载正确的 cldr-data 文件(我知道这并不总是准确的,但在这种情况下它应该是有效的。我的操作系统设置为 en-US,浏览器具有三种语言,带有“ de" 作为navigator.languages
)的显示和列表的第一个
尽管逗号分隔符被正确识别(它接受,
并拒绝.
日期输入,但我无法弄清楚为什么日期被标记为无效。
使用 datetimepicker 选择器给了我一个无效的日期
歌剧/铬
边缘
火狐
_ValidationScriptsPartial.cshtml
<environment include="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<!-- cldr scripts (needed for globalize) -->
<script src="~/lib/cldrjs/dist/cldr.js"></script>
<script src="~/lib/cldrjs/dist/cldr/event.js"></script>
<script src="~/lib/cldrjs/dist/cldr/supplemental.js"></script>
<!-- globalize scripts -->
<script src="~/lib/globalize/dist/globalize.js"></script>
<script src="~/lib/globalize/dist/globalize/number.js"></script>
<script src="~/lib/globalize/dist/globalize/date.js"></script>
<script src="~/lib/jquery-validation-globalize/jquery.validate.globalize.js"></script>
</environment>
<environment exclude="Development">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha256-F6h55Qw6sweK+t7SiOJX+2bpSAa3b/fnlrVCJvmEj1A=">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha256-9GycpJnliUjJDVDqP0UEu/bsm9U+3dnQUH8+3W10vkY=">
</script>
</environment>
<script type="text/javascript">
function setGlobalization() {
debugger;
var url = "@Url.Action("GetBrowserLanguage", "Movies")";
$.ajax({
cache: false,
type: 'GET',
url: url,
data: { navigatorlanguage: navigator.language }
}).done(function (result) {
debugger;
$.when(
$.get("/lib/cldr-data/supplemental/likelySubtags.json"),
$.get("/lib/cldr-data/main/" + result.language + "/numbers.json"),
$.get("/lib/cldr-data/supplemental/numberingSystems.json"),
$.get("/lib/cldr-data/main/" + result.language + "/ca-gregorian.json"),
$.get("/lib/cldr-data/main/" + result.language + "/timeZoneNames.json"),
$.get("/lib/cldr-data/supplemental/timeData.json"),
$.get("/lib/cldr-data/supplemental/weekData.json")
).then(function () {
debugger;
// Normalize $.get results, we only need the JSON, not the request statuses.
return [].slice.apply(arguments, [0]).map(function (result) {
return result[0];
});
}).then(Globalize.load).then(function () {
debugger;
Globalize.locale(result.language);
});
}).fail(function (result) {
alert("Server error(setGlobalization): " + result.statusText + " Please refresh and try again");
});
}
document.addEventListener('DOMContentLoaded', function () {
setGlobalization();
}, false);
</script>
创建.cshtml
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ReleaseData" class="control-label"></label>
<input asp-for="ReleaseData" class="form-control" />
<span asp-validation-for="ReleaseData" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Genre" class="control-label"></label>
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
电影控制器.cs
public ActionResult GetBrowserLanguage(string navigatorlanguage)
{
try
{
string localePattern = "lib\\cldr-data\\main\\{0}";
var cultureToUse = "es-ES"; //Default regionalisation to use
string defaultLanguage = "es";
if (navigatorlanguage.StartsWith("es", StringComparison.InvariantCultureIgnoreCase))
cultureToUse = "es";
if (navigatorlanguage.StartsWith("de", StringComparison.InvariantCultureIgnoreCase))
cultureToUse = "de";
if (navigatorlanguage.StartsWith("en", StringComparison.InvariantCultureIgnoreCase))
cultureToUse = "en-US-POSIX";
if (!System.IO.Directory.Exists(System.IO.Path.Combine(_hostingEnvironment.WebRootPath, string.Format(localePattern, cultureToUse))))
cultureToUse = defaultLanguage;
return Json(new
{
error = false,
exception = false,
language = cultureToUse
});
}
catch (Exception ex)
{
return Json(new
{
error = true,
exception = true,
statusText = ex.Message
});
}
}
电影.cs
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseData { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
完整存储库