Unable to remove required attribute of a RadioGroup via javascript

1 Answer 276 Views
RadioGroup
Richard
Top achievements
Rank 1
Richard asked on 04 May 2022, 03:30 PM

Scenario: Drop-down for customer to select preferred language. If they choose anything other than English, the Radio Group 'Interpreter Required?' (Yes / No) would be a required field. If they select English, this Radio Group is NOT required.

I have the 'Required' Data Annotations set in my model:

[Required]
public int? LanguageID { get; set; }

[Required]
public int? InterpreterRequiredID { get; set; }


View:

<div class="form-group">
    <label>What is your preferred language?</label><br />

    @Html.Kendo().ComboBoxFor(m => m.LanguageID).BindTo(new List<SelectListItem>()
    {
        new SelectListItem() { Text="English", Value="E" },
        new SelectListItem() { Text="Spanish", Value="S" },
        new SelectListItem() { Text="Other", Value="O" }
    }).Events(e => e
        .Change("LanguageID_onChange")
    )
</div>
<div class="form-group">
    <label>Do you require an interpreter?</label><br />
    @Html.Kendo().RadioGroupFor(m => m.InterpreterRequiredID).Items(i =>
        {
            i.Add().Label("Yes").Value("1");
            i.Add().Label("No").Value("2");
        })
</div>
<script>
    function LanguageID_onChange(e) {
        var el = document.getElementById('InterpreterRequiredID');

        if (this.value() == 'E') {
            el.removeAttribute("data-val-required");
        } else {
            el.setAttribute("data-val-required", "This is required!");
        }
    }
</script>
The Radio Group is still requiring a selection upon submission, no matter the language selected. If I change the target control in the javascript function to any other type of input (such as textbox or combobox), the required attribute is successfully removed / assigned according to the language selection. It just doesn't appear to work with the RadioGroup.

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 09 May 2022, 12:59 PM

Hello Richard,

Thank you for the code snippets and details provided.

In order to achieve the desired behavior, I would recommend removing the "Required" attribute for the "InterpreterRequiredID". The ComboBox for the "LanguageID" is Required, so it should be selected for submitting the form. When the ComboBox is selected, the RadioGroup adds/removes the pointed attribute out of the box.

Attached is a sample project that I prepared for the case. It implements the approach above.

Make the needed tests locally with the project attached and let me know if further assistance is needed.

Kind Regards,
Anton Mironov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
RadioGroup
Asked by
Richard
Top achievements
Rank 1
Answers by
Anton Mironov
Telerik team
Share this question
or