I am trying to do data entry validation against several columns in my igGrid. I found a posting showing how to do custom validation and I implemented it successfully against one column in my grid. However when I try to implement it against 2 columns, I am getting a javascript error "'undefined' is null or not an object" from the validation code in the posting, referencing "_editor.validator()". I started out validating just the LOBName field, which works fine, but when I added validation for "SubLOBName" I started getting the error.
The example I followed only showed validation for one column so I'm probably doing something wrong when I add the additional column.
Here is the code for my grid:
----------------
@(Html.Infragistics().Loader() .ScriptPath(Url.Content("~/Infragistics/js/")) .CssPath(Url.Content("~/Infragistics/css/")) .Render() ) @(Html.Infragistics().Grid(Model.lobTable.AsQueryable()) .ID("LOBGrid") .UpdateUrl(Url.Action("UpdateLOBGrid")) .AutoCommit(true) .AutoGenerateColumns(false) .PrimaryKey("BusinessProcID") .Width("100%") .Columns(column => { column.For(x => x.BusinessProcID).DataType("number").HeaderText("Business Process ID").Width("33%"); column.For(x => x.LOBName).DataType("string").HeaderText("LOB Name").Width("33%"); column.For(x => x.SubLOBName).DataType("string").HeaderText("Sub LOB Name").Width("33%"); }) .Features(feature => { feature.Paging().PageSize(15); feature.Resizing(); feature.Updating().EnableAddRow(true).EditMode(GridEditMode.Cell).EnableDeleteRow(false) .ColumnSettings(settings => { //settings.ColumnSetting().ColumnKey("BusinessProcID").EditorOptions(@"validatorOptions: {errorLabel: 'GridValidation', onChange: true}").Validation(true); //settings.ColumnSetting().ColumnKey("BusinessProcID").ReadOnly(true); settings.ColumnSetting().ColumnKey("LOBName").EditorOptions(@"validatorOptions: {errorLabel: 'GridValidation', onChange: true}").Validation(true); settings.ColumnSetting().ColumnKey("SubLOBName").EditorOptions(@"validatorOptions: {errorLabel: 'GridValidation', onChange: true}").Validation(true); }); feature.Filtering(); feature.Hiding().ColumnSettings(settings => { settings.ColumnSetting().ColumnKey("BusinessProcID").AllowHiding(false); }); }) .DataBind() .Render() )
-----------------
Here is the validation BLOCKED SCRIPT
// validation scripts $.ig.loader(function () { customValidator("LOBGrid", "LOBName", validate); //alert("LOB"); customValidator("LOBGrid", "SubLOBName", validate); //alert("SUB"); }); function validate(e) { /* The e parameter has two fields: e.value - value of the field e.message - message to be shown when the value is invalid */ // MAKE YOUR CUSTOM VALIDATION HERE //alert(validateSpecialCharacters(e.value)); // call the javaScript validateSpecialCharacters function in the global.js file. var success = validateSpecialCharacters(e.value); if (success) { return true; } e.message = "The text entered, '" + e.value + "', contains invalid characters."; return success; } /* This is the custom validator function for igGridUpdating It takes parameters: gridId - the id of the grid fieldKey - key of the field to be verified validateFunction - function with signagure validateFunction(e) { e.value - value of the field e.message - message to be shown when the value is invalid } There is no need to configure the field in the updating columnSetting igGridUpdating is mandatory for this function to work */ function customValidator(gridId, fieldKey, validateFunction) { if (!$("#" + gridId).data("igGrid")) throw new Error("igGrid is not defined on element with id: " + gridId); if (!$("#" + gridId).data("igGridUpdating")) throw new Error("igGridUpdating is not defined"); var _attached = false; var columnSettings = $("#" + gridId).igGridUpdating("option", "columnSettings"); columnSettings.push({ columnKey: fieldKey, required: true, editorOptions: { validate: true } }); // add field setting to the updating columnSettings $("#" + gridId).igGridUpdating("option", "columnSettings", columnSettings); $(document).delegate("#" + gridId, "iggridupdatingeditcellstarted", function (evt, ui) { if (!_attached) { _attachCustomValidator(); _attached = true; } }); function _attachCustomValidator() { var editor = $("#LOBGrid").igGridUpdating("editorForKey", fieldKey); (function (editor) { var _editor = editor; var _validator = _editor.validator(); _validator._setOption("checkValue", function (evt, ui) { var value = _editor.value(); var args = { value: value, message: "" }; var isValid = validateFunction(args); if (!isValid) { ui.message = args.message; return false; } return true; }); })(editor.data("igEditor")); } }
------------
Thanks in advance!
Hello Paul,
I am glad that you have been able to resolve your issue.
Please feel free to contact me if you have any additional questions regarding this matter.
Thanks Vasya! That fixed my problem. I was calling my customValidator function from the $.ig.loader when I should have been calling it from a $(document).delegate function as you did in your code.
In my scenario I am creating one more custom validation function for the second column. I am calling the customValidator function twice with the two different validators - once for each column. In my scenario I am calling the function in the created event of the igGrid, which is fired when the grid is created and the initial structure is rendered. Please keep in mind that I am setting the GridEditMode to Row. For example:
@(Html.Infragistics().Grid(Model) .ID("grid1") .Height("600px") .Width("100%") .RenderCheckboxes(true) .PrimaryKey("OrderID") .AutoCommit(true) .Columns(column => { column.For(x => x.OrderID).HeaderText("OrderID"); column.For(x => x.Price).HeaderText("Price"); column.For(x => x.Quantity).HeaderText("Quantity"); }) .Features(features => { features.Updating().EditMode(GridEditMode.Row).ColumnSettings(cs => { cs.ColumnSetting().ColumnKey("OrderID").EditorOptions(@"validatorOptions: {errorLabel: 'GridValidation', onChange: true}").Validation(true); // cs.ColumnSetting().ColumnKey("Quantity").EditorOptions(@"validatorOptions: {errorLabel: 'GridValidation', onChange: true}").Validation(true); }); ; features.Selection().Mode(SelectionMode.Row).MultipleSelection(true); features.Filtering().Mode(FilterMode.Simple); features.Sorting().Mode(SortingMode.Multiple).Type(OpType.Remote); features.Paging().PageSizeDropDownLocation("inpager").PageSizeList(new List<int>(){50,100,200,500}).PageSize(3); features.ColumnMoving().Mode(MovingMode.Deferred).MoveType(MovingType.Dom); features.Hiding().ColumnChooserHideOnClick(true); features.Resizing(); }) .DataBind() .Render() ) <script type="text/javascript"> $(document).delegate("#grid1", "igcontrolcreated", function (evt, ui) { customValidator("grid1", "OrderID", validate); customValidator("grid1", "Quantity", validate1); }); function validate(e) { /* The e parameter has two fields: e.value - value of the field e.message - message to be shown when the value is invalid */ // MAKE YOUR CUSTOM VALIDATION HERE if ((e.value >= 2 && e.value <= 7) || (e.value == 0)) { // VALIDATION PASSED return true; } // VALIDATION FAILED. POPULATE A MESSAGE FOR THE USER e.message = "Please enter value between 2 and 7 or 0"; return false; } /* This is the custom validator function for igGridUpdating It takes parameters: gridId - the id of the grid fieldKey - key of the field to be verified validateFunction - function with signagure validateFunction(e) { e.value - value of the field e.message - message to be shown when the value is invalid } There is no need to configure the field in the updating columnSetting igGridUpdating is mandatory for this function to work */ function customValidator(gridId, fieldKey, validateFunction) { if (!$("#" + gridId).data("igGrid")) throw new Error("igGrid is not defined on element with id: " + gridId); if (!$("#" + gridId).data("igGridUpdating")) throw new Error("igGridUpdating is not defined"); var _attached = false; var columnSettings = $("#" + gridId).igGridUpdating("option", "columnSettings"); columnSettings.push({ columnKey: fieldKey, required: true, editorOptions: { validate: true } }); // add field setting to the updating columnSettings $("#" + gridId).igGridUpdating("option", "columnSettings", columnSettings); $(document).delegate("#" + gridId, "iggridupdatingeditrowstarted", function (evt, ui) { if (!_attached) { _attachCustomValidator(); _attached = true; } }); function _attachCustomValidator() { var editor = $("#grid1").igGridUpdating("editorForKey", fieldKey); (function (editor) { var _editor = editor; var _validator = _editor.validator(); _validator._setOption("checkValue", function (evt, ui) { var value = _editor.value(); var args = { value: value, message: "" }; var isValid = validateFunction(args); if (!isValid) { ui.message = args.message; return false; } return true; }); })(editor.data("igEditor")); } } // implementation of custom validation function function validate1(e) { /* The e parameter has two fields: e.value - value of the field e.message - message to be shown when the value is invalid */ // MAKE YOUR CUSTOM VALIDATION HERE if ((e.value >= 0 && e.value <= 1500) || (e.value == 1444)) { // VALIDATION PASSED return true; } // VALIDATION FAILED. POPULATE A MESSAGE FOR THE USER e.message = "Please enter value between 0 and 1000 or 1444"; return false; } </script>
Please let me know if you have any additional questions regarding this matter.
Thanks Vasya. Can you post a sample project where you used multiple validators on the same grid? I'd like to see how you did it and I'll see if I can replicate it.
Thank you for your patience.
I investigated you scenario further and I followed the same steps as mentioned in your previous post, I used two validators on two different columns in igGrid but I was unable to reproduce the behavior that you are describing.
Could you please provide me with a working sample of your application where this issue is reproducible. I believe you understand that it is very hard for me to investigate in the right direction without being able to run and debug the project.
This is going to be highly appreciated and would help me to find the root cause of this issue.