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

[Solved] Class validation not working?

8 Answers 287 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.
Joan Vilariño
Top achievements
Rank 2
Joan Vilariño asked on 11 May 2010, 10:58 AM
Hi there.

I have  a ViewModel class where I need to validate one property based on the value of another.

I've found a solution using a Custom Validator for the class like this:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 
public sealed class ValidateValueForFormat: ValidationAttribute 
    { 
        private const string _defaultErrorMessage = "bleh bleh bleh"
 
        private readonly object _typeId = new object(); 
 
        public ValidateValueForFormat(string formatProp, string valueProp) 
            : base(_defaultErrorMessage) 
        { 
            FormatProp = formatProp; 
            ValueProp = valueProp; 
        } 
 
        public string FormatProp 
        { 
            get
            private set
        } 
 
        public string ValueProp 
        { 
            get
            private set
        } 
 
        public override object TypeId 
        { 
            get 
            { 
                return _typeId; 
            } 
        } 
 
        public override string FormatErrorMessage(string name) 
        { 
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString); 
        } 
 
        public override bool IsValid(object value) 
        { 
            bool result = true
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); 
            int format = 
                (int)properties.Find(FormatProp, true /* ignoreCase */).GetValue(value); 
            string value = properties.Find(Valor, true /* ignoreCase */).GetValue(value); 
 
            ....... 
 
            here I check the value against the format and set "result" to true 
            if ok or false if not ok... 
            ....... 
 
            return result; 
        } 
    } 
 

Then I place my validation in the ViewModel's class header like this :

 
[ValidateValueForFormat("Format","Value",ErrorMessage="The value doesn't fit  the format needed."
public class MyViewModel 
 
  public int Format  { getset; } 
  public string Value {getset; } 
 

I bind a list of this class' objects to an ajax grid, and make it editable. When I edit the field "value" and click on update, the validation class gets called, the isvalid method returns false, the formaterrorstring method is also called, but don't matter the result, valid or invalid, the Update always gets successful and my property gets a value that wasn't supposed to get.

Does your grid support this kind of validations? I find weird that the validation actually gets called but the grid ignores the result...

Thanks!!

8 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 11 May 2010, 11:02 AM
Hello Joan VilariƱo,

I am not sure I understand your case. Ajax editing relies on TryUpdateModel which should return false if validation fails for some reason. In the case of custom validation however the grid will not be able to display the error message when ajax bound. Does this work with server binding/editing?

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
Joan Vilariño
Top achievements
Rank 2
answered on 11 May 2010, 11:18 AM
Ok... I explain it a little more. Here is my GridAction:

        [GridAction] 
        public ActionResult _editarGridCaracteristicas(long id) 
        { 
            var cara = _modelo.VMLocal.ListaCaracteristicas.FirstOrDefault(o => o.Id == id); 
            if (cara != null
            { 
                var ok = TryUpdateModel(cara); 
                // What shoudl I do if ok == false? 
            } 
            return 
                View(new GridModel<LocalViewModel.LocalCaracteristicaVM> { Data = _modelo.VMLocal.ListaCaracteristicas }); 
        } 
 
 
I have confirmed that ok gets false, cause the validation fails, what should I do when this happens? Should I return the GridModel as I'm doing atm or the GridAction should return anything else to make your grid at least not allow the "update"...
0
Atanas Korchev
Telerik team
answered on 11 May 2010, 11:23 AM
Hello Joan VilariƱo,

The grid does not currently support custom validation in ajax binding mode. That's because there is no way currently to tell what the error message was to the grid in order for it to stay in edit mode and display it. I this case the only workaround is to use server binding and editing.

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
Joan Vilariño
Top achievements
Rank 2
answered on 11 May 2010, 11:25 AM
Do you plan to implement this in a near future?
0
Atanas Korchev
Telerik team
answered on 11 May 2010, 11:29 AM
Hi Joan VilariƱo,

No, this is not scheduled for the near future (at least not for Q2 2010).

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
Joan Vilariño
Top achievements
Rank 2
answered on 11 May 2010, 11:50 AM
Ok, then I must find a solution...

My handicap is that the grid can't be serverbound since it's part of a large form that's already working in ajax mode... as a "one post" solution for a big entity.

Can u give me any directions on how to take this? any workaround like making a Custom client validation for the field? That field value must be validated different based on the value of another property... 

Any hint?
0
Atanas Korchev
Telerik team
answered on 11 May 2010, 01:21 PM
Hello Joan VilariƱo,

I am afraid I cannot think of any workaround which does not require major modifications of the grid source. I am not sure if ASP.NET MVC client validation is extensible. If it is - adding custom client validator should work with the grid 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
Andy
Top achievements
Rank 1
answered on 25 Mar 2011, 10:20 AM
I am using version 2011.1.315 and serverbinding my model to the grid.  I have also created a validation attribute as Joan has.  I believe i'm getting exactly the same problem, the validation routines are executed but my popup edit screen simply closes as though everything is ok.
Tags
Grid
Asked by
Joan Vilariño
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Joan Vilariño
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or