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.
Hello William,
Thank you for posting in our community.
In order to ensure that your issue is addressed correctly I need some clarifications about your scenario. Let me see whether I understood correctly.
You are looing for a way to validate that newly entered "Widget" cell values do not match any of the previously entered values for "WidgetCode" column and also validate that this value matches the "siteID". Based on the condition failed you would like to set different text for the error message?
Please verify whether this is your requirement and I will investigate this matter further for you.
Looking forward to hearing from you.
I was just thinking, the same way I can do [required: true], can I do [unique: true]? just curious.
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
Thanks. That is what I was looking for. I was not sure how to use the validator properly, so this helps alot.
Thanks again.
I am glad that you find my suggestion helpful.
Please let me know if you have any additional questions regarding this matter.