This is a migrated thread and some comments may be shown as answers.

Extending the Kendo UI validator to support required validation on radio buttons

1 Answer 343 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Trent
Top achievements
Rank 2
Trent asked on 24 Jan 2014, 10:00 PM
Is there a particular reason the Kendo UI validator does not support required radio button validation out of the box? Adding basic validation to the existing required rule seems relatively straightforward.

var radio = input.filter("[type=radio]");

if (radio.length) {
                //Ideally the search should be scoped to the element that validate was called on, but that would require several other changes
                return  $("input[name='" + input.attr("name") + "']").is(":checked"); 
            }

This would enable radio button validation, but would require all radio buttons in a group to specify the required attribute or the other radio buttons in the group would validate successfully, overriding the one that failed. A solution to the problem would be to do what jQuery validate does, which is to only validate the first element in an array of elements with the same name. Is there any downside to doing this? The only common uses for elements with the same name are checkboxes, radio buttons and buttons, and since buttons are already not validated, I don't see any downside.

Right now, without making the code changes specified above, I am relying on a custom rule that is incredibly inefficient since it has to continually get all radio buttons with the same name to see if any of them are decorated with a required attribute.

                    radiorequired: function (input) {
                        if (input.is("[type=radio]")) {
                            var radioButtons = $("input[name='" + input.attr("name") + "']");
                            if (!radioButtons.is("[data-val-required], required")) return true;

                            return radioButtons.is(":checked");
                        }

                        return true;
                    }

I guess my question is, why doesn't Kendo UI support required validation on radio buttons out of the box, and is there a better way to add the validation than what I am doing now?

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 28 Jan 2014, 04:00 PM
Hello Trent,

Thank you for your feedback!
The main reasons for not including a built-in radio button validation are exactly the ones you mentioned - performance and the necessity of adding the required attribute to each element in the radio button group. I would encourage you to submit this as a feature request at KendoUI UserVoice so we and other members of the community can evaluate, comment on and vote for it. Most voted requests are often included in future KendoUI releases.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Validation
Asked by
Trent
Top achievements
Rank 2
Answers by
Alexander Popov
Telerik team
Share this question
or