requiredIf 条件验证属性
我正在寻找一些关于实现执行以下操作的验证属性的最佳方法的建议。
模型
public class MyInputModel
{
[Required]
public int Id {get;set;}
public string MyProperty1 {get;set;}
public string MyProperty2 {get;set;}
public bool MyProperty3 {get;set;}
}
我希望至少有 prop1 prop2 prop3 的值,如果 prop3 是唯一填充的值,它不应该等于 false。 我将如何为此编写验证属性?
感谢您的帮助!
I was looking for some advice on the best way to go about implementing a validation attribute that does the following.
Model
public class MyInputModel
{
[Required]
public int Id {get;set;}
public string MyProperty1 {get;set;}
public string MyProperty2 {get;set;}
public bool MyProperty3 {get;set;}
}
I want to have atleast prop1 prop2 prop3 with a value and if prop3 is the only value filled it it should not equal false.
How would i go about writing a validation attribute(s?) for this?
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我昨天遇到了同样的问题,但我以一种非常干净的方式完成了它,该方式适用于客户端和服务器端验证。
条件:根据模型中其他属性的值,您希望将另一个属性设为必需。代码如下:
PropertyName
是您要为其设置条件的属性DesiredValue
是 PropertyName(属性)的特定值,您的其他属性必须对其进行验证,假设您有以下内容:
最后但并非最不重要的一点是,注册您的属性的适配器,以便它可以进行客户端验证(我将其放在 global.asax,Application_Start 中)
编辑
有些人报告了客户端无论如何都会触发的问题或它不起作用。因此,我修改了上面的代码,以使用 Javascript 进行条件客户端验证。对于这种情况,您不需要注册适配器
,最后是 javascript(将其捆绑并渲染...将其放入自己的脚本文件中)
您显然需要根据要求包含不显眼的验证 jQuery
I had the same problem yesterday, but I did it in a very clean way which works for both client side and server side validation.
Condition: Based on the value of other property in the model, you want to make another property required. Here is the code:
PropertyName
is the property on which you want to make your conditionDesiredValue
is the particular value of the PropertyName (property) for which your other property has to be validated for requiredSay you have the following:
At last but not the least, register adapter for your attribute so that it can do client side validation (I put it in global.asax, Application_Start)
EDITED
Some people have reported issues that the client side fires no matter what or it does not work. So I modified the above code to do conditional client side validation with Javascript as well. For this case you don't need to register adapter
And finally the javascript ( bundle it and renderit...put it in its own script file)
You need obviously the unobstrusive validate jQuery to be included as requirement
我知道这个话题不久前就被问过,但最近我遇到了类似的问题,并找到了另一个,但在我看来,这是一个更完整的解决方案。我决定实现提供条件属性的机制,以根据逻辑表达式中定义的其他属性值及其之间的关系来计算验证结果。
使用它,您可以通过以下方式实现您所询问的结果:
有关 ExpressiveAnnotations 库的更多信息可以在此处找到。它应该简化许多声明性验证案例,而无需编写额外的特定于案例的属性或在控制器内部使用命令式验证方式。
I know the topic was asked some time ago, but recently I had faced similar issue and found yet another, but in my opinion a more complete solution. I decided to implement mechanism which provides conditional attributes to calculate validation results based on other properties values and relations between them, which are defined in logical expressions.
Using it you are able to achieve the result you asked about in the following manner:
More information about ExpressiveAnnotations library can be found here. It should simplify many declarative validation cases without the necessity of writing additional case-specific attributes or using imperative way of validation inside controllers.
我让它在 ASP.NET MVC 5 上工作,
我看到很多人对这段代码感兴趣并深受其苦,我知道这是第一次,这确实令人困惑和混乱。
Notes
修复此错误的提示
提示#1:确保您继承自“ValidationAttribute”而不是“RequiredAttribute”
提示#2:或者从“Global.asax”中删除这一整行,在新版本的代码中根本不需要它(经过@Dan_Hunex编辑后) ,是的,这一行在旧版本中是必须的...
使 Javascript 代码部分工作的提示
1- 将代码放入新的 js 文件中(例如:requiredIfValidator.js)
2- 扭曲a 里面的代码$(文档).ready(function(){........});
3- 在包含 JQuery 验证库后包含我们的 js 文件,所以现在看起来像这样:
编辑 C# 代码
到
My Entire Code Up
4-
and Running
Global.asax
此处无需添加任何内容,保持干净
requiredIfValidator.js
在 ~/content 或 ~/scripts 文件夹中创建此文件
_Layout.cshtml 或 View
RequiredIfAttribute.cs Class
在项目中的某个位置创建它,例如在 ~/models/customValidation/
模型
视图
这里没有什么需要注意的实际上...
我稍后可能会为此上传一个项目示例...
希望这有帮助
谢谢
I got it to work on ASP.NET MVC 5
I saw many people interested in and suffering from this code and i know it's really confusing and disrupting for the first time.
Notes
Tips To Fix This Error
Tip #1: make sure that you're inheriting from 'ValidationAttribute' not from 'RequiredAttribute'
Tip #2: OR remove this entire line from 'Global.asax', It is not needed at all in the newer version of the code (after edit by @Dan_Hunex), and yes this line was a must in the old version ...
Tips To Get The Javascript Code Part Work
1- put the code in a new js file (ex:requiredIfValidator.js)
2- warp the code inside a $(document).ready(function(){........});
3- include our js file after including the JQuery validation libraries, So it look like this now :
4- Edit the C# code
from
to
and from
to
My Entire Code Up and Running
Global.asax
Nothing to add here, keep it clean
requiredIfValidator.js
create this file in ~/content or in ~/scripts folder
_Layout.cshtml or the View
RequiredIfAttribute.cs Class
create it some where in your project, For example in ~/models/customValidation/
The Model
The View
Nothing to note here actually ...
I may upload a project sample for this later ...
Hope this was helpful
Thank You
如果您尝试使用“ModelState.Remove”或“ModelState[“Prop”].Errors.Clear()”,“ModelState.IsValid”仍然返回 false。
为什么不直接从模型中删除默认的“必需”注释,并在控制器“发布”操作上的“ModelState.IsValid”之前进行自定义验证?像这样:
If you try to use "ModelState.Remove" or "ModelState["Prop"].Errors.Clear()" the "ModelState.IsValid" stil returns false.
Why not just removing the default "Required" Annotation from Model and make your custom validation before the "ModelState.IsValid" on Controller 'Post' action? Like this:
扩展了 Adel Mourad 和 Dan Hunex 的注释,我修改了代码以提供一个仅接受不与给定值匹配的值的示例。
我还发现我不需要 JavaScript。
我将以下类添加到我的模型文件夹中:
我不需要对我的视图进行任何更改,但确实对我的模型的属性进行了更改:
希望这有帮助!
Expanding on the notes from Adel Mourad and Dan Hunex, I amended the code to provide an example that only accepts values that do not match the given value.
I also found that I didn't need the JavaScript.
I added the following class to my Models folder:
I didn't need to make any changes to my view, but did make a change to the properties of my model:
Hope this helps!
与其他解决方案的主要区别在于,该解决方案在服务器端重用了
RequiredAttribute
中的逻辑,并使用required
的验证方法depends
属性在客户端:The main difference from other solutions here is that this one reuses logic in
RequiredAttribute
on the server side, and usesrequired
's validation methoddepends
property on the client side:我认为使用 IValidatableObject 是一个不错的选择。
I think using IValidatableObject is a good choice.