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

Display Validation Message Based on DropDown Selection

5 Answers 1012 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Marcab
Top achievements
Rank 1
Veteran
Marcab asked on 20 Jul 2019, 05:24 PM

I have an application which has two kinds of donors: individuals and organizations. Each donor type has different required fields. When organization is selected as the donor type, I want to prompt the user to select an organization type before adding/saving.

Organization type is required for organizations.

I'm looking for a simple example of how to do this.

Thanks in advance.

5 Answers, 1 is accepted

Sort by
0
Petar
Telerik team
answered on 24 Jul 2019, 05:54 AM
Hi,

Attached to this reply you will find an example demonstrating the desired functionality. The activation of the DropDownList validation is realized with this setRequiredAttr function:
function setRequiredAttr(evt) {
    if (evt.sender.element[0].value === "1") {
        console.log("value is 1");
        $("#OrganizationType").removeAttr("data-val-required");
    }
    else {
        console.log("value is 2");
        $("#OrganizationType").attr("data-val-required", "This field is required, please choose an option.");
    }
}

The above code is being executed on change event in the DropDownList from which we select the donor type – individual or organization. Depending on this selection we add or remove the data-val-required attribute from the input of the second DropDownList (the one for selecting the organization type).

Please check the proposed implementation and let me know if you have any questions regarding it. 
Looking forward to your reply. 
 
Regards,
Petar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Marcab
Top achievements
Rank 1
Veteran
answered on 26 Jul 2019, 05:17 PM
Thank you Petar. I really appreciate the reply, and I'll let you know how it turns out.
0
Petar
Telerik team
answered on 30 Jul 2019, 03:17 PM
Hi Marcab,

Take your time to try the proposed implementation and let me know if you need further assistance. 

Regards,
Petar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Marcab
Top achievements
Rank 1
Veteran
answered on 31 Jul 2019, 12:38 PM

Your solution works well except for one minor issue.

If I create an Individual type record after having created an Organization type record with the required organization type, it's possible to save that individual record with the organization type of the previously saved record. Ideally, I'd like to reset the organization drop-down after the creation of a record (since it would be fairly easy for a user to save an organization with the previous type (which could be incorrect).

I'm working on this now.

Thanks again for your help.

0
Petar
Telerik team
answered on 02 Aug 2019, 09:12 AM
Hi Marcab,

On form submit you can select the default value of the DropDownList using the select method like it is demonstrated in the following snippet:
$("#OrganizationType").kendoDropDownList().data("kendoDropDownList").select(1);

Regards,
Petar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DropDownList
Asked by
Marcab
Top achievements
Rank 1
Veteran
Answers by
Petar
Telerik team
Marcab
Top achievements
Rank 1
Veteran
Share this question
or