对隐藏字段进行验证 [英] Perform validation on hidden fields

查看:222
本文介绍了对隐藏字段进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下看法:

    @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)

所有属性都被标记为 [必需的]在我看来,模型。然后,问题是,我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.

我的问题:是否有可能在一个隐藏字段进行验证?一种解决方法存在吗?

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

这可能看起来有点怪来验证隐藏字段。其原因是,该字段从jQuery的基于特殊规则填充。如果不填,我知道的东西是不是在视图中有效。

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.

感谢。

推荐答案

可能的原因可能是有忽略::隐藏在 jquery.validate.unobtrusive.js 文件。

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

1.9.0版本后,它是默认的行为。您可以手动固定通过添加

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

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

正如你所看到的这里

另一个变化应该与隐藏要素的形式设置
  更容易,这些都是现在默认(忽略的选择忽略了
  :隐藏现在默认
的)。在理论上,这可能会破坏现有的
  建立。在不可能的情况下,它实际上呢,您可以修复它
  设置忽略选项为[](方括号无
  引号)。

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).

这篇关于对隐藏字段进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆