Set Initial Value for Required attribute

风格不统一 提交于 2019-12-23 23:46:06

问题


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.


回答1:


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.




回答2:


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?




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6902187/set-initial-value-for-required-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!