设置必需属性的初始值

发布于 2024-11-27 14:40:30 字数 232 浏览 2 评论 0原文

使用数据注释指定模型的验证规则,我想设置必需属性的初始值,以便在它等于该值时失败。这是使用验证控件的网络表单中的一个选项,但我没有看到类似的使用数据注释。

如果没有内置的方法来做到这一点,我可能只会构建一个继承它的新的必需属性。

更新:根据评论/答案,我绝对知道这不是理想的情况,我应该使用 HTML5 的占位符属性。但是,我需要对已经以非理想方式添加阴影文本的现有表单执行此操作。

Using Data Annotations to specify validation rules for a model, I want to set the initial value for the Required attribute so that it fails if it's equal to that value. This was an option in webforms using the validation controls, but I don't see similar using Data Annotations.

If there's no built in way to do it, I'll probably just build a new Required Attribute that inherits from it.

UPDATE: Based on comments/answers, I definitely know this is not an ideal situation and I should be using the placeholder attribute of HTML5. However, I need to do this for an existing form that already had shadow text being added in a non-ideal way.

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

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

发布评论

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

评论(4

蝶舞 2024-12-04 14:40:30

我不会在 MVC 中处理这个问题,我会努力让阴影文本不与表单一起发布。 HTML 5 有“占位符”。我会使用它或实现类似的东西: http://davidwalsh.name/html5-placeholder

检查一下这个简单快速的占位符方法,该方法可以在非 html5 浏览器中正确降级:

JQuery 占位符 HTML5 模拟器

只需将占位符属性添加到文本框并使用该 javascript 即可使其正常工作,无论HTML 版本。

I wouldn't handle that in MVC, I'd work on getting the shadow text to not post with the form. HTML 5 has "placeholder". I'd use that or implement something like it: http://davidwalsh.name/html5-placeholder

Check this out for a simple quick placeholder approach that degrades properly in non-html5 browsers:

JQuery placeholder HTML5 simulator

Just add the placeholder attribute to your textboxes and use that javascript to make it work regardless of HTML version.

旧人九事 2024-12-04 14:40:30

我不知道是否有任何内置注释可用于 notequalto,但您可以创建自己的 NotEqualAttribute,继承 ValidationAttribute (用于验证服务器)并实现 IClientValidatable (生成 data-something 标记,在客户端上进行验证)。

此外,您还必须在 jquery 中编写代码以进行客户端验证。

请参考此

DataAnnotations 在 MVC 中真正如何工作?

I dont know there is any built in annotations available or not for notequalto, but you can create your own NotEqualAttribute, inheritValidationAttribute (for validation on server) and implement IClientValidatable (to produce data-something tags, to do validation on client).

Also you have to write code in jquery for client validation.

refer this

How does DataAnnotations really work in MVC?

仲春光 2024-12-04 14:40:30

我最终创建了一个继承自RequiredAttribute的新属性:

public class RequiredWithInitialValueAttribute : RequiredAttribute
{
    public string InitialValue { get; set; }

    public RequiredWithInitialValueAttribute(string initialValue)
    {
        InitialValue = initialValue;
    }

    public override bool IsValid(object value)
    {
        if (value.ToString().SafeEquals(InitialValue)) return false;
        return base.IsValid(value);
    }
}

仍然好奇是否有更好的方法,因为这似乎应该是内置的,但现在我正在使用它。

I ended up creating a new attribute that inherits from the RequiredAttribute:

public class RequiredWithInitialValueAttribute : RequiredAttribute
{
    public string InitialValue { get; set; }

    public RequiredWithInitialValueAttribute(string initialValue)
    {
        InitialValue = initialValue;
    }

    public override bool IsValid(object value)
    {
        if (value.ToString().SafeEquals(InitialValue)) return false;
        return base.IsValid(value);
    }
}

Still curious if there's a better way since this seems like something that should be built in, but for now I'm using this.

不醒的梦 2024-12-04 14:40:30

不要这样做。

占位符文本不是值。不应发布它。。它不应该被验证。
这违背了常识,是危害人类罪,并带来了一些问题。

  • 如果您需要带有占位符文本的可选数字字段怎么办?
    (验证将失败,因为占位符是字符串。)

  • 本地化您的应用程序后会发生什么?
    (验证将会失败,直到有人发现正在与此属性值进行比较。)

  • 验证必须仅在服务器上进行,并且无法将占位符文本“灰显”。
    (除非您在脚本文件中再次指定占位符并监视更改事件。)

  • 您必须在不同的位置至少指定此“占位符”两次。< /strong>
    (如果我没理解错的话,它一次在属性中,一次在构造函数中。)


  • 占位符实际上无法成为值。< br>
    (如果不是针对此字段,那么对于使用该属性的任何其他字段,都可能发生这种情况。)

  • 这会让未来的维护者感到害怕。
    (你还怀疑吗?)

相反,

使用 placeholder 属性和降级解决方案 按照 Milmetric 的建议

作为焦躁不安的 DYI-ers 的替代方案,将标签放置在 div 中,并使用您自己的 JavaScript 将其放置在输入字段上。这称为“overlabel”技术,网上有很多示例。 (但是您必须在想要支持的浏览器上彻底测试您的解决方案。)您甚至可以推出自己的 自定义 HTML 帮助器:(

@Html.PlaceholderFor(m => m.SomeField)

我完全编造了它,由你来实现它。)

Don't do this.

Placeholder text is not a value. It should not be posted. It should not be validated.
This contradicts common sense, is a crime against humanity and introduces several problems.

  • What if you need an optional number field with a placeholder text?
    (Validation will fail because placeholder is a string.)

  • What happens when your application is localized?
    (Validation will fail until someone finds this attribute value is being compared with.)

  • Validation will have to be server-only and there will be no way to “grey out” placeholder text.
    (Unless you specify the placeholder once more in the script file and monitor change events.)

  • You will have to specify this “placeholder” at least twice, and in different places.
    (If I got you right, it's once in the attribute and once in the constructor.)

  • There will be no way for placeholder to actually be the value.
    (This can happen, if not for this field, then for any other that uses the attribute.)

  • This will freak the hell out of the future maintainers.
    (Do you even doubt that?)

Instead,

Use placeholder attribute and degrading solutions as suggested by Milimetric.

As an alternative for restless DYI-ers, place the label in a div and position it over the input field with your own JavaScript. This is known as “overlabel” technique and there are plenty examples on the web. (But you'll have to test your solution thoroughly on the browsers you want to support.) You may even roll out your own custom HTML helper:

@Html.PlaceholderFor(m => m.SomeField)

(I completely made it up, it's up to you to implement it.)

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