I have the following in side an HTML page:
<!-- Template for simple search row -->
<
script
id
=
"simple-search-row"
type
=
"text/x-kendo-template"
>
<
div
class
=
"simple-search-row"
>
<
input
name
=
"fieldName"
data-role
=
"dropdownlist"
data-text-field
=
"columnDisplayName"
data-value-field
=
"columnName"
data-bind="
value: field,
source: columnsDataSource"></
input
>
<
span
data-for
=
"fieldName"
class
=
"k-invalid-msg"
></
span
>
<
input
name
=
"filter"
data-role
=
"dropdownlist"
data-text-field
=
"displayName"
data-value-field
=
"name"
data-bind="
value: type,
source: filterTypesDataSource,
events: { change : criteriaChanged, dataBound : simpleSearchDDLDataBound }"
style
=
"width: 15em;"
></
input
>
<
span
data-for
=
"filter"
class
=
"k-invalid-msg"
></
span
>
<
input
id
=
"value-1"
name
=
"value-1"
type
=
"text"
data-bind="
value: values[0]"></
input
>
<
span
data-for
=
"value-1"
class
=
"k-invalid-msg"
></
span
>
<
span
id
=
"value-and"
style
=
"display: none;"
>#: translate('between_and') #</
span
>
<
input
id
=
"value-2"
name
=
"value-2"
type
=
"text"
style
=
"display: none;"
data-bind="
value: values[1]"></
input
>
<
span
data-for
=
"value-2"
class
=
"k-invalid-msg"
></
span
>
<
a
href
=
"\\\\#"
data-bind
=
"events: { click : removeSimpleSearchRow }"
>
<
span
class
=
"k-icon k-i-cancel"
></
span
>
</
a
>
<
a
href
=
"\\\\#"
data-bind
=
"events: { click : addSimpleSearchRow }"
>
<
span
class
=
"k-icon k-i-plus"
></
span
>
</
a
>
</
div
>
</
script
>
<
div
id
=
"simple-search-rows"
class
=
"ui relaxed grid"
data-template
=
"simple-search-row"
data-bind
=
"source: searchFilter.criteria"
></
div
>
"searchFilter.criteria" is a JS array object. And then I have a custom validator:
var
simpleSearchValidator = $(
'#simple-search'
).kendoValidator({
messages : {
fieldname :
''
,
filter :
''
,
valueone :
''
,
valuetwo :
''
},
rules: {
fieldname :
function
(input) {
if
(input.is(
'[name|=fieldName]'
)) {
if
(input.val() ===
''
){
return
false
;
}
}
return
true
;
},
filter :
function
(input) {
if
(input.is(
'[name|=filter]'
)) {
if
(input.val() ===
''
){
return
false
;
}
}
return
true
;
},
valueone :
function
(input) {
if
(input.is(
'[name|=value-1]'
)) {
let filter = input.parent().find(
'[name|=filter]'
),
criteria = (filter ? filter.val() :
''
);
switch
(criteria){
case
'EQUAL_TO'
:
case
'NOT_EQUAL_TO'
:
return
true
;
}
if
(input.get(0).style.display ===
'none'
) {
return
true
;
}
if
(input.val() ===
''
){
return
false
;
}
}
return
true
;
},
valuetwo :
function
(input) {
if
(input.is(
'[name|=value-2]'
) && input.is(
''
)) {
if
(input.val() ===
''
){
return
false
;
}
}
return
true
;
}
}
}).data(
'kendoValidator'
);
The issue is that the validator's .validate() is called, it correctly identifies incorrect data based on the custom rule, however if say the fieldName input field is invalid, it displays the k-invalid-msg for EVERY row, not just the single row with the invalid data. I am guessing this is because the name attributes for each row are not unique. I tried creating a unique id in the template:
<!-- Template for simple search row -->
<
script
id
=
"simple-search-row"
type
=
"text/x-kendo-template"
>
<
div
class
=
"simple-search-row"
>
# var uid = windows.util.genUniqueId(); #
<
input
name
=
"fieldName#= uid #"
data-role
=
"dropdownlist"
data-text-field
=
"columnDisplayName"
data-value-field
=
"columnName"
data-bind="
value: field,
source: columnsDataSource"></
input
>
....
</
div
>
</
script
>
<
div
id
=
"simple-search-rows"
class
=
"ui relaxed grid"
data-template
=
"simple-search-row"
data-bind
=
"source: searchFilter.criteria"
></
div
>
Unfortunately, that did not work as the uid was only created once, at the time the template was loaded, not for each row.
Is there an easy way to generate a unique form name for the input elements in each row?