使用派生类和数据注释的软验证

发布于 2024-12-17 16:44:01 字数 1001 浏览 2 评论 0原文

今天有一个很酷的想法,但我不知道如何实施。

我正在 asp.net MVC3 C# 中逐页构建一个表单,该表单保存到每个页面末尾的临时区域。

现在这些数据进入一个类,里面有一些可枚举的类,所以它相对复杂。

现在,我希望用户能够在没有验证的情况下输入他们想要的任何内容(显然安全性除外),以便他们可以输入他们想要的任何内容,但是我希望这是软验证的。

我想使用元数据进行验证,但是由于这提供了硬验证,我无法将其放在表单用作模型的类上(强类型)。因此,我认为最好的方法是从表单上的类派生一个类,并在其中添加数据注释(我希望可以以某种方式应用接口,但我假设不是)

那么当用户想要将表单提交到我们的数据库时,我希望表单能够通过并验证。我想通过将没有保存数据的数据注释的类转换为带有 dataAnnotations 的空类来实现这一点,并且发生的任何错误(基本上是当数据无效时)将能够在屏幕上列出并告诉他们来修复它。

现在,我想做的另一件事是,当用户使用表单时,我可以以某种方式使用带注释的类来软验证表单。所以我可以说“我们建议您放入此字段”。而不是“该字段不能为空”。

所以,总而言之:

  1. 是否可以打开和关闭接口,如果可以,如何打开和关闭?
  2. 我如何将 classA 转换为 classB,其中 classB 派生自 classA 并
  3. 在转换期间使用 dataAnnotations(续 2),我如何捕获任何数据错误并列出它们
  4. 如何在模型所在的表单上软验证 classB classA

我希望这一切都是可能的。这似乎是最干净的方法。如果有人能想出更好的方法来做到这一点,我会洗耳恭听......或眼睛。


更新1 我的印象是数据注释会阻止我输入任何不正确的数据。但是我目前正在做一些测试,似乎我能够以编程方式应用不正确的数据。即必填字段为空。那么数据注释只适用于页面上的验证吗?如果是这样,请忽略我的整个问题。


更新2 如果有人可以贡献,我仍在寻找答案

Got a pretty cool idea today but I'm not sure how to go about it.

I'm building a page by page form in asp.net MVC3 C#, that saves to a temporary area at the end of each page.

Now this data goes into a class with a few enumerable classes in it, so its relatively complicated.

Now, I want the user to be able to put in whatever the hell they want without validation (except security obviously), so that they can throw in whatever they want, however i want this to be soft validated.

I would like to use metadata for validation, however since this provides hard validation I can't put this on the class that the form uses as a model (strongly typed). So, what i think is the best way to do it is to derive a class from the class on the form, and throw data annotations all over that (I'm hopeful that its possible to somehow apply an interface, but I'm assuming it isn't)

THEN when the user wants to submit the form to our database, i want the form to go through and validate. I figure ill do this by converting the class without the data annotations that holds the data into the empty one with the dataAnnotations, and any errors that occur(essentially when the data isn't valid) ill be able to list on the screen and tell them to fix it.

Now, another thing that i want to be able to do would be that i could somehow use the annotated class to soft validate the form when the user is on it. So i can say "we recommend you put this field in". Rather than, "this field can't be empty".

So, in conclusion:

  1. Is it possible to turn interfaces on and off, if so, how?
  2. How would i convert classA into classB where classB is derived from classA and uses dataAnnotations
  3. (Continuation of 2) during the conversion, how would i catch any data errors and list them
  4. How could i soft validate off of classB on a form where the model is classA

I hope this is all possible. It seems like the cleanest way to do it. If anybody can think of a better way to do this, i am all ears... or eyes.


Update 1
Im under the impression that dataAnnotations would prevent me from putting any incorrect data in. However I'm doing some testing at the moment, and it seems I am able to apply incorrect data programatically. Ie null in a required field. So do data annotations only apply to validation on the page? If so, disregard my entire question.


Update 2
Im still looking for an answer if anybody can contribute

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2024-12-24 16:44:01

我不认为你可以使用 MVC 中的验证框架来做到这一点。验证是一个二元运算;输入要么有效,要么无效。用于指示验证过程中成功或失败的 ValidationResult 类没有“Maybe”成员(使用术语),它要么是,要么不是。

也就是说,由于您可以控制整个验证过程,因此您始终可以定义自己的数据注释属性集,并在控制器中测试我认为是“警告”的内容,并做出相应的反应,也许可以使用通知区域显示诸如“我们建议您在此字段中输入数据”或类似内容的字符串。

这是假设您没有使用不引人注目的客户端验证,我不知道如何规避。我谈论使用控制器中的 ModelState 变量的方式可以超越 IsValid 测试。

I don't think you can do this with the validation framework in MVC. Validation is a binary operation; input is either valid or it isn't. The ValidationResult class used to indicate success or failure during the validation process doesn't have a 'Maybe' member (to use a term), it's either yes or not.

That said, since you have control over the entire validation process, you can always define your own set of data annotation attributes and in your controller(s) test for what I assume would be 'warnings', and react accordingly, maybe by using a notification area that displays strings like 'we recommend you enter data in this field" or something like that.

This is assuming that you're not using unobtrusive client-side validation, which I wouldn't be sure how to circumvent. I'm talking about using the ModelState variable in your controllers in a way that you can go beyond the IsValid test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文