Problem: I have a kendoui ComboBox that is setup for auto-complete with a Primary Data Source (the record being edited) and a Secondary DataSource (the data for the auto-complete). The Primary Data Source has a schema with the field:
'primaryDS_SelectedItemID': {
type: 'number',
defaultValue: null,
nullable: false
}
that the selected items value in the comboBox binds to. Now if I enter a value in the comboBox that does not exist in the secondary data source, the value of primaryDS_SelectedItemID will be null, which is correct. But if I then validate the form with the kendoValidator it passes. But I don't want it to. I need a rule to say if null or empty that checks the row of the primary datasource that is currently being edited.
I have made an attempted at creating this rule, but the issue is how do I get the row that is currently being edited?
I have a put together a simple example that demonstrates the problem.
HTML:
<div id="form" style="padding:10px">
<label>Item:</label>
<input id="comboBox"
name="Item" data-role="combobox"
data-placeholder="Type an Item Name"
data-value-primitive="true"
data-text-field="secondaryDS_Name"
data-value-field="secondaryDS_ID"
data-filter="startswith"
data-auto-bind="false"
data-bind="value: currentDataView.primaryDS_SelectedItemID"
required />
<span class="k-invalid-msg" data-for="Table Name"></span>
<button id="validate">Validate</button>
</div>
JavaScript:
$(document).ready(function () {
var oViewModel = {
currentDataView: {},
primaryDataSource: new kendo.data.DataSource({
schema: {
model: {
id: 'primaryDS_ID',
fields: {
'primaryDS_ID': {
type: 'number'
},
'primaryDS_SelectedItemID': {
type: 'number',
defaultValue: null,
nullable: false
}
}
}
}
}),
secondaryDataSource: new kendo.data.DataSource({
data: [
{ secondaryDS_ID: 1, secondaryDS_Name: 'Item One' },
{ secondaryDS_ID: 2, secondaryDS_Name: 'Item Two' },
{ secondaryDS_ID: 3, secondaryDS_Name: 'Item Three' }
],
total: 3,
schema: {
model: {
id: 'secondaryDS_ID',
fields: {
'secondaryDS_ID': {
type: 'number'
},
'secondaryDS_Name': {
type: 'string'
}
}
}
}
})
},
oValidator = $("#form").kendoValidator().data("kendoValidator"),
oComboBox = null;
oViewModel.currentDataView = oViewModel.primaryDataSource.add();
kendo.init($('#comboBox'));
oComboBox = $('#comboBox').data('kendoComboBox');
oComboBox.setDataSource(oViewModel.secondaryDataSource);
kendo.bind(oComboBox, oViewModel);
$('#validate').on('click', function () {
oValidator.validate();
});
$('#logRecord').on('click', function () {
$('#status').text(JSON.stringify(oViewModel.currentDataView));
});
});
Fiddle: http://jsfiddle.net/codeowl/Mq6ee/5/
Replication Steps:
1) Press the Validate button and you will see that the validation works on an empty value.
2) Enter the value: "test" into the combo box and then press the Validate button. You will see that validation does not work on a null value.
3) Press the Log Record button and you will see that the value of primaryDS_SelectedItemID is null.
Thank you for your time,
Regards,
Scott