0

I am trying to validate dropdown list using FluentValidation. I think I am doing wrong, but not able to find to solution. I need your help.

Model

 [FluentValidation.Attributes.Validator(typeof (MeetingAbstractValidator))]
 public class MeetingAbstract
 {
     public string CompanyAllowedBoolLabel { get; set; }
     public string CompanyAllowedBoolOptions { get; set; }
     public string CompanyAllowedBoolText { get; set; }
     ...
 }



class MeetingAbstractValidator : AbstractValidator<MeetingAbstract>
{
     public MeetingAbstractValidator()
     {
        RuleFor(x => x.CompanyAllowedBoolText).NotEqual("Select Option...").
        WithMessage(i18n_Models_Abstract.RequiredField); ;

  }
}

view

<div class="row" style="padding-bottom: 10px">
    <div class="col-md-10">
        <div class="col-md-6">@Html.Label(Model.CompanyAllowedBoolLabel, 
               new { @class = "control-label mandatory" })</div>
        <div class="col-md-4">
            @{

                var options = Model.CompanyAllowedBoolOptions;
                var optionsList = options.Split(',').ToList();
                var optionSelect = optionsList.Select(option => 
                 new SelectListItem() { Text = option, Value = option }).ToList();
                optionSelect.Insert(0,new SelectListItem() {
                             Text = "Select Option...", Value = "0" });
            }
            @Html.DropDownListFor(model => model.CompanyAllowedBoolText, 
                optionSelect,
                 new { @class = "input-validation-error form-control" })
            @Html.ValidationMessageFor(model => model.CompanyAllowedBoolText)
        </div>
    </div>
</div>

Generated HTML

<div class="col-md-4">
   <select class="form-control valid" id="CompanyAllowedBoolText"   
                              Name="CompanyAllowedBoolText">
          <option value="0">Select Option...</option>
          <option value="да/yes">да/yes</option>
          <option value="нет/no">нет/no</option>
    </select>
    <span class="field-validation-valid" data-valmsg-for="CompanyAllowedBoolText"
       data-valmsg-replace="true"></span>
 </div>

Its working for textboxes. not dropdowns.

James123
  • 11,184
  • 66
  • 189
  • 343

3 Answers3

3

make sure that value is empty for select option

optionSelect.Insert(0,new SelectListItem() { Text = "Select Option...", Value = "" });

and then add this

RuleFor(x => x.CompanyAllowedBoolText).NotEmpty().WithMessage("your error message");

Rohidas Kadam
  • 428
  • 5
  • 12
0

Part of the problem is that you shouldn't be validating specific kinds of controls. Validating a dropdown list should be no different from validating a textbox, it's all about the selected value. When a form posts a DropDownList to the server, it only posts the value, not the text. You're checking for the text.

Another part of the problem is that you are not correctly building your drop down list. You should use the built-in functionality for the unselected value. I'm not sure why you're trying to include validation css classes. You shouldn't be. ie:

@Html.DropDownListFor(model => model.CompanyAllowedBoolText, optionSelect, 
     "Select Option...")

In this case, if the user does not select anything, the value will be either null or empty. So you need only use a required validation. Remember, the text doesn't get posted, only the value.

RuleFor(x => x.CompanyAllowedBoolText).NotNull().NotEmpty().
    WithMessage(i18n_Models_Abstract.RequiredField);
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

From your generated html (and drop down creation code), posted values of CompanyAllowedBoolText can be

  • 0
  • да/yes
  • нет/no

So the option's VALUE , not the option's "text".

So your rule should check for

RuleFor(x => x.CompanyAllowedBoolText).NotEqual("0").<bla bla bla>();
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122