简体   繁体   中英

jQuery unobtrusive validation on dynamically generated radio buttons in MVC3?

I've got a dynamic forms app and am trying to apply proper unobtrusive validation for the form elements and am having trouble with getting the form to display validation errors the way I want.

My partial view to render the radio button form items looks like this:

@model FormItem

<div class="form-row">
<div class="form-item">@Model.Text</div>
<div class="form-item-responses">
    @foreach(FormItemResponse formItemResponse in Model.Responses.OrderBy(x => x.SortOrder))
    {
        if(Model.Required)
        {
            @Html.RadioButton(Model.Id.ToString(), formItemResponse.Id, formItemResponse.DefaultSelection, new { @class = "required", data_val = "true", data_val_required = "*"}) @formItemResponse.Text

        }
        else
        {
             @Html.RadioButton(Model.Id.ToString(), formItemResponse.Id, formItemResponse.DefaultSelection) @formItemResponse.Text
        }

        <text>&nbsp; &nbsp;</text>
    }
    <span class="field-validation-valid" data-valmsg-for="@Model.Id.ToString()" data-valmsg-replace="true"></span>
</div>

Here is the final markup for those not familiar with MVC:

<div class="form-row">
<div class="form-item">Form Item Text Here</div>
<div class="form-item-responses">
    <input class="required" data-val="true" data-val-required="*" name="026d44a7-fa55-4fe8-8d2f-4f561c77c716" type="radio" value="dcfa4a9a-53e1-44d5-b6b3-a133673bfa2e" />Yes            &nbsp; &nbsp;
    <input class="required" data-val="true" data-val-required="*" name="026d44a7-fa55-4fe8-8d2f-4f561c77c716" type="radio" value="0042876b-2362-4f65-9c8a-dddf7f8206e8" />No            &nbsp; &nbsp;
    <input class="required" data-val="true" data-val-required="*" name="026d44a7-fa55-4fe8-8d2f-4f561c77c716" type="radio" value="a0918eab-93b6-4e45-a78d-301e28571037" />NA            &nbsp; &nbsp;
    <span class="field-validation-valid" data-valmsg-for="026d44a7-fa55-4fe8-8d2f-4f561c77c716" data-valmsg-replace="true"></span>
</div>

In works in that any group of radio buttons get properly validated, and the * comes up next to the item. However, what I would like to do is change the text color of everything in .form-item-responses to red. Not just the validation error message.

How can I do this? I tried using the invalidHandler like so:

 $("#items-form").validate({
      invalidHandler: function (form, validator) {
          var errors = validator.numberOfInvalids();
          console.log(errors);
          $("input.input-validation-error").each(function() {
              this.parent().css("color", "red");
          });
      }
  });

But that appears to override the default behavior and just put a black This field is required next to the first radio button in each answer group. I want the default behavior and then just this extra change.

The way I fixed this was getting the validator from the form as a variable, then accessing the settings in document.ready like so:

        var validatorData = $('#items-form').data('validator');
        validatorData.settings.highlight = function (element, errorClass) {
            $(element).parent().parent().css("color", "red");
        };

I am not sure why the first way I tried it didn't work. As that seems to be the way in the documentation and on many examples online.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM