html.validationsummary 样式,jQuery.validate and Html.ValidationSummary playing nice together

jQuery.validate and Html.ValidationSummary playing nice together

05/25/2010

3 minutes to read

In this article

A customer recently asked me how to get MVC with the MVC Futures project’s MicrosoftMvcJQueryValidation.js adapter file to play nicely with the Validation Summary HTML helper. It turns out that this functionality isn’t built into the adapter script file.

They also pointed me at this post by Soe Tun that gets things up and running. Soe changes the content of the script file to add this behaviour, relies on some non-public jQuery.validate functionality, and adds a custom Html.ValidationSummaryJQuery.

As I’m guessing that the adapter script will evolve to deal with this in the future and I don’t want the fact that I’m using jQuery to affect my server code, I felt the need to take a slightly different approach. What I really wanted was the ability to “patch” the behaviour right now with nothing more than a client-side only separate patch script, and to delete my patch script when it is no longer required, presumably also replacing the adapter script at the same time. I also wanted to avoid any features that aren’t public if at all possible.

The Solution

In the end I got this working by overriding the defaults for the validation library’s showErrors function. I had to use the approach of setting the defaults as I couldn’t actually intercept the construction and initialisation of the validator. The showErrors function only lists errors found during the current sweep of validation (and if that was triggered by an onblur event that will only ever be the current field) so I also had to get a list of all fields that are already marked invalid by scanning for those with a CSS class of “.input-validation-error”, and to combine that with the showErrors argument contents.

The end result is some script that looks like this;

// change the default showErrors behaviour

$.validator.setDefaults({

showErrors: function (errorMap, errorList) {

// Extracts existing field validation errors that

// are not necessarily part of the current validation phase.

// It's important to note that errorMap and errorList

// only contain failures from the current validation phase,

// so for example when you tab out of a field only that

// field is validated, and hence errorList will only

// ever contain a single error.

// Therefore this script combines this result with any

// previous failures already being displayed.

var map = {};

$('.input-validation-error').each(function () {

var element = this;

var txt = $('#' + element.id + '_validationMessage').text();

map[element.name] = txt;

});

// add the errors from this validation phase

// map will ensure they're unique as it's keyed on control name

$.extend(map, errorMap);

// convert all these unique errors into a nice simple array

var messages = [];

for (var item in map) {

messages[messages.length] = map[item];

}

// remove existing errors

clearErrors();

// display error list or hide validation summary

messages.length ? displayErrors(messages) : displaySuccess();

// let jQuery highlight the fields

this.defaultShowErrors();

}

This main function uses a few helper functions that are included in the download.

Of course there were also other issues – the adapter script “pops” the MVC metadata off an array so I have to interrogate that before the pop occurs, so the script must be in the right place (i.e. it must be called before document.ready).

Conclusion

All this adds up as a bit of a hack still and is by no means perfect; that’s why the jQuery adapter is in the Futures project and not the core product yet I guess. I still thought it was worth blogging though in case you find it useful – make sure you check out Soe’s approach too in case you prefer it.

I’ve attached an example solution so that you can see it in action and grab the full script. I’ve named the script “jquery-validate-mvc-patch.js”.

Anyone got a better solution than either of these? Do please feel free to propose something better as I’m not completely satisfied with this!

* Note this is a repost following the migration of our blogging platform

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值