Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

"Require" validation (data annotation) not working for select list

Open rahuldhariya01 opened this issue 11 years ago • 9 comments

No error comes when we apply require attribute to Select list in mvc 6.

Posted issue at http://forums.asp.net/t/2056134.aspx?Select+list+require+validation+mvc+6+using+data+annotation

rahuldhariya01 avatar Jun 22 '15 16:06 rahuldhariya01

Copying from the forum:

How to apply validation to select list in mvc 6 ( i need to implement data annotation validation )

for exaple in model i have applied "Required" attribute to "year" filed.

for example : code

<select asp-for="Year">
<option selected value=""></option>
<option value="2015">2015</option>
<option value="2014">2014</option>

</select>

<span asp-validation-for="Year"></span>

Other fields validation are working fine but for select item it is not working at all. can you give me a live example ?

dougbu avatar Jun 22 '15 17:06 dougbu

The [Required] attribute meant something different than its MSDN documentation in earlier versions of MVC. We have corrected that bug in MVC 6. Use [BindRequired] to ensure a struct field has a value.

In this case your model likely includes

  class MyModel
  {
    ...
    [Required]
    public int Year { get; set; }
  }

Change that to

  class MyModel
  {
    ...
    [BindRequired]
    public int Year { get; set; }
  }

[BindRequired] is in the Microsoft.AspNet.Mvc.ModelBinding namespace.

dougbu avatar Jun 22 '15 17:06 dougbu

Please update your forum issue and close it if this answers your question.

dougbu avatar Jun 22 '15 17:06 dougbu

Thanks Dougbu, But it seems [BindRequired] is not working with me .

    [BindRequired]
    public string Year { get; set; }


    [Required(AllowEmptyStrings =true,ErrorMessage = "Text Box value Required")]
    public string Name { get; set; }

Issue-

  1. Using BindRequired how we can set error message like Required?
  2. BindRequired itself not working, i can give you source code to you.

Do you have some documentation for this ?

rahuldhariya01 avatar Jun 22 '15 19:06 rahuldhariya01

@rahuldhariya01

  1. Overriding the error message is not currently supported.
  2. Which MVC Beta are you using? The source code might help me see the issue faster.

Do you have some documentation for this ?

[BindRequired] is described fairly completely as "model binding will add a ModelState error for properties with this attribute that are not present in a submission`. Other than that, not much documentation (yet).

dougbu avatar Jun 22 '15 21:06 dougbu

@dougbu Appreciate your help.

PFA (https://drive.google.com/file/d/0B_t4iGqASEkeN0tCSlV2UDQ2b2M/view?usp=sharing)

  1. Validation to select list
  2. how to set selected value from server side (C#)
  3. BindRequired is not working.

Go to Home/index.

rahuldhariya01 avatar Jun 22 '15 21:06 rahuldhariya01

@Dougbu, any update on this issue ?

rahuldhariya01 avatar Jun 23 '15 18:06 rahuldhariya01

@rahuldhariya01 Your Beta 4 application shouldn't need any change from the original. I incorrectly thought the Year property was an int but it's string. [Required] does what you want for string properties in all versions of MVC 6, including Beta 4. ([BindRequired] is more important in Beta 5 and Beta 6 but is not relevant for your project.)

What problems remain when using [Required]?

FYI I took your sample and changed the HomeModel back (I assume) to

        [Required]
        public string Year { get; set; }

The generated HTML is now:

    <select data-val="true" data-val-required="The Year field is required." id="Year" name="Year">
        <option value="">Select Now </option>
        <option>2016</option>
        <option>2017</option>
    </select>
    <span class="field-validation-valid" data-valmsg-for="Year" data-valmsg-replace="true"></span>

And client-side validation means I get errors if I hit Submit without filling in the Year field.

dougbu avatar Jun 24 '15 23:06 dougbu

Thanks for help.

rahuldhariya01 avatar Jun 25 '15 15:06 rahuldhariya01