使用 xVal 验证必须为 true 的 bool
我有一个业务要求,要求在允许提交表单之前强制将 HTML 表单上的复选框标记为 true。
如果尚未使用适当的消息选中此框,但希望同时从表单数据的 xVal 验证中返回所有信息,我可以将用户返回到表单。
我在其他地方找不到任何信息,因此是否可以使用 xVal 来验证 bool 为 true(或 false),类似于使用 [Range(min, max)]
DataAnnotation 或者我必须手动 .AddModelError(..)
包含此信息以将错误添加到 ViewModel?
I have a business requirement to enforce a check box on a HTML form to be marked as true before allowing submission of the form.
I can return the user to the form if this box has not been checked with an appropriate message, but want to return all information from an xVal validation of the form data at the same time.
I can't find any information elsewhere, so is it possible to use xVal to validate a bool to true (or false), similar to using the [Range(min, max)]
DataAnnotation or must I manually .AddModelError(..)
containing this information to add the error to the ViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过创建自己的 ValidationAttribute?我为这种情况创建了一个 TrueTypeAttribute。
Have you tried creating your own ValidationAttribute? I created a TrueTypeAttribute for this sort of situation.
xVal 处理复选框上的必填字段数据注释,因为必须选中。我最近必须解决这种情况,因为我试图表示一个不可为空的布尔值,其中复选框可以为真或为假(只是不为空)。但就你而言,这很完美。但是,它提供了必填字段验证消息,您可能正在寻找“必须接受这些条款”类型的消息。
使用 xval 远程规则验证并从 ajax 资源进行验证可能是最简单的。
xVal treats a required field dataannotation on a checkbox, as must be checked. I had to work around this situation recently as I was trying to represent a non-nullable boolean where the checkbox could be true or false (just not null). But in your case this works out perfectly. However, it gives a required field validation message where you are perhaps looking for a "must accept these terms" type message.
It might be easiest to use the xval remote rule validation, and validate from an ajax resource.