This is a migrated thread and some comments may be shown as answers.

Custom unobtrusive validation

8 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nicholas
Top achievements
Rank 1
Nicholas asked on 12 Aug 2013, 03:36 PM
Hi,

I cannot seem to find any information on how to get custom MVC 4 unobtrusive validation to work on individual records within a grid (client-side). Server-side validation works correctly, but the client validation function is never called. For built-in MVC validations such as the [Required] attribute it works fine on both the server and client, but any validations I've created myself do not.

I know the grid uses Kendo Validator internally, but I am not sure how to register my custom validation functions with it, if it's not supposed to just pick them up from the ones I've registered with jQuery validate like so:

$.validator.addMethod('dategreaterthan', function(value, element, params)
{
    return dateValidateCore('dategreaterthan', value, element, params, this.currentForm.id);
}, '');
I don't think there's anything wrong with my custom attribute since it works on standard HTML form elements, but I guess I need to do something differently to make Kendo aware of it.

Are there any examples of doing this that I've missed? The documentation doesn't seem to cover using this with the Grid at all.

8 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 14 Aug 2013, 09:04 AM
Hello Nicholas,

 I prepared a demo showing how to use custom validators with the grid: http://jsbin.com/ekuwip/1/edit

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nicholas
Top achievements
Rank 1
answered on 14 Aug 2013, 02:25 PM
Hi Atanas,

Thanks for your response.

I saw similar code on this page but wasn't quite clear on how to use it with a grid created by the MVC extensions. My solution was to set the field's validation property in the DataBound event:

this.fields["FieldName"].validation = { valid : validationFunction };

function validationFunction(field)
{
  field.attr('data-valid-msg', 'Field is required.');
  return false;
}
Not sure if this is the proper way but it seemed to work. However, I was hoping that the grid could pick up the unobtrusive validation automatically instead of setting a validation function explicitly on each field. Is this not supported by Kendo?

The second problem that I ran into with this is that Kendo seems to block editing when there are validation errors. I have some fields that need to be validated based on other columns in the grid; for example Field 1 might be a dropdown, and Field 2 is an input that may or may not be required based on the value selected in Field 1. If I check that Field 2 is filled in when the value of Field 1 is changed and return false if not, a validation error will appear on Field 1 and the user won't be able to change Field 2 until they reset the value in Field 1 again (which would be somewhat tedious). Can this behavior be disabled?

Thanks,
Nick

0
Accepted
Daniel
Telerik team
answered on 16 Aug 2013, 08:59 AM
Hello,

In order to use the custom attribute in the Grid, you should extend the Kendo Validator with the rule. We have a demo(Custom validator editing) that demonstrates this scenario in the examples project included with the installation. 
I am not sure if I understand correctly the scenario with editing the other field. Are you using InCell editing? If yes, then you should use a custom validation rule instead of the built-in required validation in order to allow the other field to be edited when no value is set and return true. 

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nicholas
Top achievements
Rank 1
answered on 20 Aug 2013, 06:22 PM
Thanks Daniel. I hadn't realized some of the offline demos weren't available on the site. The code in the one you pointed out worked perfectly.

Might I suggest adding this to the online demos? Seems like something a lot of people would be looking for.

Thanks,
Nick
0
Daniel
Telerik team
answered on 22 Aug 2013, 01:37 PM
Hello,

Thank you for the feedback. We will consider adding an online custom validation demo for the next release.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
mark baer
Top achievements
Rank 1
answered on 13 Aug 2014, 05:48 PM
Daniel, I reviewed the example you described.  It is setup for HTML/JavaScript instantiation.  Are there any samples for the Razor/MVC implementation?  Thanks
0
mark baer
Top achievements
Rank 1
answered on 14 Aug 2014, 04:33 PM
Atanas, good stuff.  We are using Razor though.  Do you know how I would implement that for my Grid definition in Razor/MVC?

Thanks
0
Daniel
Telerik team
answered on 15 Aug 2014, 12:47 PM
Hello Mark,

How to use a custom validation rule is demonstrated in the custom validator demo and is also described in the validation documentation.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Nicholas
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Nicholas
Top achievements
Rank 1
Daniel
Telerik team
mark baer
Top achievements
Rank 1
Share this question
or