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

ValidationMessage for dropdownlist

5 Answers 1682 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Federico
Top achievements
Rank 1
Federico asked on 19 Jan 2018, 11:38 AM

I would like to create a validation message only for dropdownlist.

I'd like to get the text value of label.

label is parent of kendo dropdownlist

var custom = function(input){
    if(input.is("[data-role=dropdownlist]")){
        if(Utils.isNullOrUndefined(input[0].value) || input[0].value.length === 0){
            return true;
        }
        return input.data("kendoDropDownList").value();
    }
    return true;
    }
 
    rules.custom = custom;
    messages.custom = function(input){
        "Insert" + input.closest("label").text()
    }
         
    var validator = {
        validate: function(){
            $(".k-invalid:first").focus();
        },
        rules: rules,
        messages: messages
    };
     
    return $("#form1").kendoValidator(validator).data("kendoValidator");

 

This solution doesn't work

 

 

 

5 Answers, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 22 Jan 2018, 04:26 PM
Hello Federico,

In order to set the text from the label as message of the custom rule, you can use the following
messages: {         
          custom: function(input){
                return $(input).parents().find('label').text();
          }         
 },

I would suggest you to change the structure of the input element and its label, and set them to be siblings
<label for="time">Start time  </label>
<input id="time" name="time">

In case the label is parent of the input, by calling the text() method on the label, the text from the input field (text in the DropDownList) will be also retrieved.

I hope you will find the linked Dojo example helpful.


Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Federico
Top achievements
Rank 1
answered on 23 Jan 2018, 08:49 AM
if I wanted to do it to only those that have the parameter 'required', how should I do?
0
Ivan Danchev
Telerik team
answered on 25 Jan 2018, 07:27 AM
Hello Federico,

You can validate only those with required attribute by checking whether this attribute is present:
if (input.is("[required][data-role=dropdownlist]")) {

The dojo example with this modification added.

Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Federico
Top achievements
Rank 1
answered on 25 Jan 2018, 09:10 AM

This solution doesn't work.

It show:" time is required"

I'd like to show the custom message ("home" for example) for the required dropdownlist.

 

0
Ivan Danchev
Telerik team
answered on 26 Jan 2018, 03:36 PM
Hi Federico,

Here's the example modified to show custom message and validate only the required DropDownList.

Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Validation
Asked by
Federico
Top achievements
Rank 1
Answers by
Neli
Telerik team
Federico
Top achievements
Rank 1
Ivan Danchev
Telerik team
Share this question
or