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

Validation incorrectly showing up when initial value in multiselect

2 Answers 298 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 18 Oct 2019, 07:46 PM

When using a MultiSelectFor, I am having a problem with kendo validator if there is already a value in the model for that multiselect.

 

For example:

Below is the code for the MultiSelectFor. On my form I am checking to see if a property of a model exists, if so I am adding the validator with the message "Required" to it. It is my understanding that this should only occur if the multiselect is empty. Attached is a picture of the required validator showing when there is a selection in the multiselect. The problem is when there is an initial value on the first load, after that it works as intended where it is removed when a value is selected and is shown when there is no value.

In the attachment you will see that it is only this multiselect that has a problem. everything else works fine with an initial value.

How do I go about making sure that if there is an initial value that the validator is not shown? Is there an event that fires after the value is populated so that I can run a validation function to clear out the validation?

01.validateForm: function() {
02.    var validator = $('#' + this.formName).kendoValidator({
03.        messages: {
04.            required: "Required"
05.        }
06.    }).data("kendoValidator");
07. 
08.    if (validator != null)
09.        return validator.validate();
10.    else return null;
11.}

 

 
@(Html.Kendo().MultiSelectFor(m => m.SelectedResources).DataTextField("Text").DataValueField("Value").Placeholder("No Resources Selected").DataSource(source => { source.Read("ReadActiveUsers", "_System");}))

2 Answers, 1 is accepted

Sort by
0
Accepted
Aleksandar
Telerik team
answered on 22 Oct 2019, 02:55 PM

Hello Brent,

The observed issue occurs because the validation for the MultiSelect is triggered before data is bound to the widget. 

You could resolve the issue by calling the validation function in the DataBound event handler for the MultiSelect. This event is fired when the widget is bound to data from its data source:

function onDataBound(e) {
    validator.validate();
}

Give the above suggestion a try and let me know if any of these approaches work for you. In case of further questions please get back to me.

Regards,
Aleksandar
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
n/a
Top achievements
Rank 1
answered on 22 Oct 2019, 03:17 PM

Aleksander,

The above solution worked. Thank you!

Tags
MultiSelect
Asked by
n/a
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
n/a
Top achievements
Rank 1
Share this question
or