FluentValidation基于MVC4中的下拉选择的规则依赖性验证

时间:2014-06-02 16:04:16

标签: c# validation asp.net-mvc-4 fluentvalidation

我需要根据dropdownselect验证两个文本框。它是依赖性验证。为此,我使用FluentValidation。关键是这里,dropdownlist的值是语言规范。 (俄罗斯,西班牙语)。

RuleFor(x => x.HasMaterialPublishedElseWhereText).NotEmpty().WithMessage(i18n_Models_Abstract.RequiredField);
RuleFor(x => x.DtPublishedTimeText).NotEmpty().When(x => x.HasMaterialPublishedElseWhereText == i18n_Models_Abstract.AbstractYes).WithMessage(i18n_Models_Abstract.RequiredField);
RuleFor(x => x.PublishedPlaceText).NotEmpty().When(x => x.HasMaterialPublishedElseWhereText == i18n_Models_Abstract.AbstractYes).WithMessage(i18n_Models_Abstract.RequiredField);

视图

 <div class="row" style="padding-bottom: 10px">
  <div class="col-md-10">
   <div class="col-md-4">
      @Html.HiddenFor(model => model.HasMaterialPublishedElseWhereOptions)
      @{
           options = Model.HasMaterialPublishedElseWhereOptions;
           optionsList = options.Split(',').ToList();
           optionSelect = optionsList.Select(option => new SelectListItem() { Text = option, Value = option }).ToList();
        }
        @Html.DropDownListFor(model => model.HasMaterialPublishedElseWhereText, optionSelect, i18n_Models_Abstract.SelectOption +"...", new { @class = "input-validation-error form-control" })
        @Html.ValidationMessageFor(model => model.HasMaterialPublishedElseWhereText)
    </div>
   </div>
 </div>
 <div class="row" style="padding-bottom: 10px">
   <div class="col-md-10">
     <div class="col-md-4">
        @Html.TextBoxFor(model => model.DtPublishedTimeText, new { @class = "form-control", @placeholder = Model.DtPublishedTimeLabel, maxlength = 40 })
        @Html.ValidationMessageFor(model => model.DtPublishedTimeText)
      </div>
 </div>
</div>
<div class="row" style="padding-bottom: 10px">
  <div class="col-md-10">
     <div class="col-md-4">
         @Html.TextBoxFor(model => model.PublishedPlaceText, new { @class = "form-control", @placeholder = Model.PublishedPlaceLabel, maxlength = 40 })
          @Html.ValidationMessageFor(model => model.PublishedPlaceText)
      </div>
   </div>
</div>

HTML

  <div class="col-md-4">
       <input id="HasMaterialPublishedElseWhereOptions" name="HasMaterialPublishedElseWhereOptions" type="hidden" value="да/yes,нет/no">
       <select class="input-validation-error form-control" data-val="true" data-val-required="обязательное поле" id="HasMaterialPublishedElseWhereText" name="HasMaterialPublishedElseWhereText">
    <option value="">Выбрать опции...</option>
    <option selected="selected" value="да/yes">да/yes</option>
    <option value="нет/no">нет/no</option>
</select>
    <span class="field-validation-valid" data-valmsg-for="HasMaterialPublishedElseWhereText" data-valmsg-replace="true"></span>
  </div>
  <div class="col-md-4">
     <input class="form-control" id="DtPublishedTimeText" maxlength="40" name="DtPublishedTimeText" placeholder="Если Да, укажите дату публикации/If yes, Date" type="text" value="">
     <span class="field-validation-valid" data-valmsg-for="DtPublishedTimeText" data-valmsg-replace="true"></span>
   </div>
<div class="col-md-4">
    <input class="form-control" id="PublishedPlaceText" maxlength="40" name="PublishedPlaceText" placeholder="" type="text" value="">
    <span class="field-validation-valid" data-valmsg-for="PublishedPlaceText" data-valmsg-replace="true"></span>
  </div>

enter image description here

所有其他验证都有效,除了这个依赖关系不起作用?什么事都错了?

1 个答案:

答案 0 :(得分:0)

依赖验证不会生成适当的html属性,因为只有在用户输入后才能知道依赖项值(true或false)。

jquery.validate 框架(基于客户端验证)和 MVC模型元数据基础架构(用于可转换为客户端的简单规则)边规则),不支持多个属性集,因此,MVC基础设施决定不生成任何属性。

相同的逻辑适用于 RuleSets 自定义:在指定规则集下定义的规则不参与验证属性生成。