Remote validation with AdditionalFields when editing the Grid InCell mode

1 Answer 243 Views
Grid
serge
Top achievements
Rank 2
Bronze
Iron
Iron
serge asked on 24 May 2021, 08:54 AM
Why the AdditionalFields are not passed along in the validation when InCell mode? SO question here

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 27 May 2021, 08:39 AM

Hi Serge,

Thank you for the shared SO thread.

By design, the Remote Validation mechanism is used for form validation. However, the grid's data cells could be validated by sending their values to the validation controller via an AJAX call.

In order to send additional fields along with the field that needs to be validated, you could add them as properties of the "data" object. The example below illustrates how to send the values of the fields "Code" and "Id" to the Action method "IsCode_Available" in the Grid Controller (the grid is in "InCell" edit mode):

<script> $(document).ready(function () { $.extend(true, kendo.ui.validator, { rules: { remote: function (input) { if (input.is("[name=Code]") && input.val()) { // var grid = $("#grid").data("kendoGrid"); //an instance of the grid var dataItem = grid.dataItem($(input).closest('tr')); //get the dataItem var recordID = dataItem.Id; //get the value of field "Id" var recordCode = input.val(); //get the value of the validated field (i.e "Code") $.ajax({ url: '@Url.Action("IsCode_Available", "Grid")', type: "POST", async: false, data: { "Code": recordCode, "id": recordID}, success: function (data) {...}, error: function () {...} }); return true } return true } }, messages: { remote: function (input) {...} } }); }); </script>

//Model

[Remote("IsCode_Available", "Grid", AdditionalFields = nameof(Id))]
public string Code
{

get;
set;
} //Controller [AcceptVerbs("GET", "POST")] public IActionResult IsCode_Available([Bind(Prefix = nameof(Model.Code))] string Code, [Bind(Prefix = nameof(Model.Id))] int id) { ... }

In addition, attached you can find:

  • a runnable demo project based on this example;
  • a video that demonstrates how the values of fields "Code" and "Id" are passed from the client to the Action method.

Would you please review them and let me know if the suggested approach works as expected at your end?

 

Regards, Mihaela Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
serge
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Mihaela
Telerik team
Share this question
or