Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
420
IGGrid RowEditDialog Custom Validation / Error Messages
posted

I am using the following to validate a field in the RowEditDialog.

columnKey: "Widget",
editorType: "text",
required: true,
valdation: true,
editorOptions: {
validatorOptions:
{
required: {
errorMessage: "You must enter a widget code to continue."
},
onblur: true,
custom:
function (ui, evt) {
var value = ui;
var rows = $("#widgetList").igGrid("rows");
for (var i = 0; i < rows.length; i++) {
var currentRow = rows[i];
var currentValue = $("#unitsList").igGrid("getCellValue", $(currentRow).attr("data-id"), "WidgetCode");

if (value.toString().toLowerCase() == currentValue.toString().toLowerCase() && $(currentRow).find(".ui-iggrid-editingcell").length == 0) {
console.log('existing unit code');
document.getElementById("errorMessage").value = "The unit code specified already exists.";
return false;
}
}
document.getElementById("errorMessage").value = "";

var siteId = document.getElementById("siteID").value;
if (!value.toString().toLowerCase().match(siteId.toLowerCase())) {
document.getElementById("errorMessage").value = "You must specify the widget Site ID in the unit code.";
console.log('wrong site id');
return false;
}

document.getElementById("errorMessage").value = "";
return true;
},
errorMessage:
function (arg) {
return document.getElementById("errorMessage").value;
}

}
}

I am using one custom method to validate two different things. I want to make sure the widget id does not already exist and I want to make sure the widget site id is correct.

I want the error message to read different things depending on what fails.Do I have to create two different custom methods, if so how do I do that? Or can I do what I am doing, is so how do I change the error message.

As you can see I have a bit of a hack where I set a hidden field to the error message then I call a function that gets the message, however this has proven to be unreliable. After the user corrects the first error, the message stays the same if the second condition fails.

Any help would be greatly appreciated.

Regards.

Parents Reply
  • 17590
    Verified Answer
    Offline posted in reply to William Reyes

    Hello William,

    Thank you for getting back to me. 

    Changing validator`s error message based on some condition can be achieved by setting errorMessage option in the custom validating function. Additionally, "required" option should be set only in the validatorOptions (the one set in the column setting for this column should be removed because it will override the one set in the validatorOptions). For example:

    						columnSettings: [
    							{
    								columnKey: "Widget", 
    								editorType: "text",
    								valdation: true,
    								editorOptions: {
    								validatorOptions:
    									{
    										required: {
    											errorMessage: "You must enter a widget code to continue."
    										},
    										onblur: true,
    										custom:
    										function (ui, fieldOptions) {
    											var validator = $("#grid").igGridUpdating("editorForKey", "Widget").data("igValidator").element;
    											if(ui === "val"){
    												$(validator).igValidator("option","errorMessage", "val");
    												return false;
    											}
    											if (ui === "val1") {
    												$(validator).igValidator("option", "errorMessage", "val1")
    												return false;
    											}
    											return true;
    										}
    								
    									}
    								}
    							}
    						]

    I created a small sample illustrating my suggestion for your reference. In the custom validator function for the "Widget" column editor`s value is checked, if it is "val" the error message is set to "val", if it is "val1" the error message is set to "val1". When the field is empty the required error message is displayed. Please test it on your side and let me know whether it helps you achieve your requirement.

    In regards to the "unique" option we do not provide it of of the box.

    Please do not hesitate to contact me if you have any additional questions regarding this matter.
    6813.igGridChangeValidationMessage.zip

Children