简体   繁体   中英

Telerik ASP.NET MVC Checkbox Kendo validation

I have boolean property in my view model wit [Required] attribute.

I have

@(Html.Kendo().CheckBoxFor(m => m.AcceptTermsOfUseAndPrivacyPolicy)

$("form").kendoValidator();

in my view.

All other controls on forms validates as expected, only check box validation is not working (validation is not performed).

What I am missing?

For MVC part (creating custom attribute, view and model validation)

http://blog.degree.no/2012/03/validation-of-required-checkbox-in-asp-net-mvc

For JavaScript part (custom Kendo validator):

(function($, kendo) {
    $.extend(true, kendo.ui.validator, {
        rules: {
            booleanrequired: function(input, params) {
                if ($(input).is(':checkbox')) {

                    var attr = $(input).attr('data-val-booleanrequired');
                    var req = $(input).attr('data-val-required');

                    if ((typeof req !== typeof undefined && req !== false) && (typeof attr !== typeof undefined && attr !== false)) {

                        return $(input).is(':checked');
                    }
                    return true;
                }
                return true;
            }
        },
        messages: {
            booleanrequired: function(input) {
                return input.attr("data-val-booleanrequired");
            }
        }
    });
})(jQuery, kendo);

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