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

[Solved] Grid client side custom validation

4 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mark Davis
Top achievements
Rank 1
Mark Davis asked on 23 Jun 2010, 10:11 PM
Hi there - not sure if I'm making a repeat post as I got a server error when I made it the last time.. apologies if so

Anyhow here goes again.  I am trying to use client side validation, but do it in a custom manner.  By this I mean check some stuff in the save action and validate by e.g. adding to the ViewData.ModelState.  This is because the property being validated is a string and the format depends on external factors so Data Annotations don't work. e.g. something like this pseudo code:

[GridAction] 
        public ActionResult _SaveAnObjectByAjax(int ID, int ObjectTypeID, string TextValue) 
        { 
     
var ObjectType = rep.GetObjectType(ObjectTypeID); 
 
 
 var regex = new Regex(ObjectType.ValidationExpressionString); 
    if (!regex.IsMatch(TextValue)) 
        ViewData.ModelState.AddModelError("TextValue""That sucks! Try something like " + ObjectType.ExampleString); 
 
Return(new GridModel(rep.All()))} 

Not too optimistic as I can't see how it would fit with your jquery validation but hey I'm new to this MVC stuff and would be great if you guys had any suggestions. Excellent extensions by the way.

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 24 Jun 2010, 06:55 AM
Hello Mark Davis,

Currently this will not work. Using ModelState is not supported in ajax editing scenarios for the time being. The only possible solution is to use server binding. The build-in jquery validation works there as well.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mark Davis
Top achievements
Rank 1
answered on 24 Jun 2010, 02:06 PM
I suspected as much.. thanks anyhow!
0
Mark Davis
Top achievements
Rank 1
answered on 26 Jun 2010, 03:33 PM
Hi there - I think I managed to kludge something that works ok for me - just sharing it here in case it's of use to anyone else.

To recap the problem was that I wanted a different kind of validation than the dataannotation could offer - i.e. I want to be able to validate using a regex that changes depending on the record chosen.. but it could equally be handyin any case were you want to validate by comparing two fields. 

I added a custom editortemplate for the element that I wanted to validate and it has the following code (no laughing.. I'm a jquery noob):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %> 
<%= Html.TextBox(null) %> 
<script type="text/javascript"
    var $textbox = $('#Value'); 
    var $tr = $textbox.closest('tr:has(form)'); 
    var grid = $tr.closest('.t-grid').data('tGrid'); 
    var dataItem = grid.dataItem($tr); 
    var $label = $('#ParamDescription'); 
    var myform = $textbox.closest('form'); 
    var validator = myform.validate(); 
     
    $label.hide(); 
    $label.append(dataItem.TestParameter.Description).slideDown().fadeIn; 
 
    $('.t-grid-update').click(function() { 
        if (dataItem.TestParameter.ValidationRegEx != null) { 
            var re = new RegExp(dataItem.TestParameter.ValidationRegEx); 
            if ($textbox.val().match(re)) { 
                return true; 
            } 
            else { 
                //alert('The format is not correct.'); 
                validator.showErrors({ "Value": "Value is incorrect format" }); 
                return false; 
            } 
        } 
        else { 
            return true; 
        } 
    });     
</script> 
<div id="ParamDescription"
</div> 

seems to work fine but having a little trouble using the same validation format (class='input-validation-error') as you guys - this is just using the default message. Fiddling around with the __MVC_EnableClientValidation method but haven't got my head around it yet. What's the neatest way to do it.?  

thanks,

Mark.   
0
sasho
Top achievements
Rank 1
answered on 03 May 2013, 07:52 PM
This is an old post but answering your question as your old post helped me with something. 

Add a an empty div tag with class of "input-validation-error" and add inner html to the div when your validation fails. That should give what your looking
Tags
Grid
Asked by
Mark Davis
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Mark Davis
Top achievements
Rank 1
sasho
Top achievements
Rank 1
Share this question
or