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

DataAnnotation Validation Error on not shown properties

8 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex Martin
Top achievements
Rank 1
Alex Martin asked on 20 Feb 2013, 02:19 PM
Is it possible to show DataAnnotation validation error on property not shown on the GridView? Maybe at the row level.  I want to show that the selected item cannot be deleted.

8 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 21 Feb 2013, 02:45 PM
Hello,

This is a built-in feature of RadGridView. I've just tested it and everything work as expected.
Just add a property to your business object something like following:

[Range(5, 10)]
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                if (this.age != value)
                {
                    this.age = value;
                    this.OnPropertyChanged("Age");
                }
            }
        }

And validation should be shown on a row level. You can take a look at the attached image. All the best,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex Martin
Top achievements
Rank 1
answered on 21 Feb 2013, 03:01 PM
Yes it shows but only if the validation rule fail during the gridview load or if the row is getting edited within the gridview.

If the property get changed outside the gridview and the value fail validation, the grid view does not show the error until the row get edited within the gridview.
0
Alex Martin
Top achievements
Rank 1
answered on 26 Feb 2013, 12:36 PM
Anyone has suggestion?
0
Nedyalko Nikolov
Telerik team
answered on 26 Feb 2013, 03:46 PM
Hello,

All you have to do is to implement INotifyPropertyChanged interface for your business object.
I've just tested and everything works as expected.
Let me know if this does not help.

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex Martin
Top achievements
Rank 1
answered on 26 Feb 2013, 04:18 PM
I do implement INotifyPropertyChanged in my Business Object...

public class City : INotifyPropertyChanged
   {
       private int id;
 
       private string name;
 
       private ObservableCollection<City> cityList;
 
       private bool isDeleted;
 
 
       [IsUniqueAttribute]
       public int Id
       {
           get
           {
               return this.id;
           }
           set
           {
               this.id = value;
               OnPropertyChanged("Id");
           }
       }
 
       [Required]
       public string Name
       {
           get
           {
               return this.name;
           }
           set
           {
               this.name = value;
               OnPropertyChanged("Name");
           }
       }
 
       public ObservableCollection<City> CityList
       {
           get
           {
               return this.cityList;
           }
           set
           {
               this.cityList = value;
               OnPropertyChanged("CityList");
           }
       }
 
       [MustBeFalseAttribute]
       public bool IsDeleted
       {
           get
           {
               return this.isDeleted;
           }
           set
           {
               this.isDeleted = value;
               OnPropertyChanged("IsDeleted");
           }
       }
 
       public event PropertyChangedEventHandler PropertyChanged;
 
 
       protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
       {
           PropertyChangedEventHandler handler = this.PropertyChanged;
           if (handler != null)
           {
               handler(this, args);
           }
       }
 
       private void OnPropertyChanged(string propertyName)
       {
           this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
       }
 
 
       public class IsUniqueAttribute : ValidationAttribute
       {
           protected override ValidationResult IsValid(object value, ValidationContext validationContext)
           {
               City instance = validationContext.ObjectInstance as City;
 
               if (instance != null)
               {
                   bool exists = instance.CityList.Any(c => c.Id == instance.Id && !c.Equals(instance));
 
                   if (exists)
                   {
                       return new ValidationResult("Not unique");
                   }
                   else
                   {
                       return ValidationResult.Success;
                   }
               }
 
 
               return base.IsValid(value, validationContext);
           }
       }
 
 
       public class MustBeFalseAttribute : ValidationAttribute
       {
           protected override ValidationResult IsValid(object value, ValidationContext validationContext)
           {
               City instance = validationContext.ObjectInstance as City;
 
               if (instance != null)
               {
 
                   if (instance.IsDeleted)
                   {
                       return new ValidationResult("Must be False");
                   }
                   else
                   {
                       return ValidationResult.Success;
                   }
               }
 
 
               return base.IsValid(value, validationContext);
           }
       }
 
   }


My ViewModel also does.

public class MyViewModel : INotifyPropertyChanged
   {
       private ObservableCollection<City> cities;
 
 
 
 
       public MyViewModel()
       {
 
 
           this.Cities = new ObservableCollection<City>();
 
           this.Cities.Add(new City { Id = 1, Name = "Chicago", CityList = this.Cities });
           this.Cities.Add(new City { Id = 2, Name = "New York", CityList = this.Cities });
           this.Cities.Add(new City { Id = 2, Name = "Miami", CityList = this.Cities });
           this.Cities.Add(new City { Id = 3, Name = "San Francisco", CityList = this.Cities, IsDeleted = true });
 
           //this.FetchDcfViewModels();
       }
 
       public ObservableCollection<City> Cities
       {
           get
           {
               return this.cities;
           }
           set
           {
               this.cities = value;
               OnPropertyChanged("Cities");
           }
       }
 
       
 
      
 
 
       public event PropertyChangedEventHandler PropertyChanged;
 
 
       protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
       {
           PropertyChangedEventHandler handler = this.PropertyChanged;
           if (handler != null)
           {
               handler(this, args);
           }
       }
 
       private void OnPropertyChanged(string propertyName)
       {
           this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
       }
   }

The error shows for "San Francisco" at load.

When I set "Chicago" to IsDeleted to True in code. The row does not show the error until I edit the city name for the "Chicago" row directly in the gridview.

private void Button1_Click(object sender, RoutedEventArgs e)
{
    ((MyViewModel)this.DataContext).Cities[0].IsDeleted = true;
}

0
Nedyalko Nikolov
Telerik team
answered on 27 Feb 2013, 07:31 AM
Hi,

I'm attaching a modified version of the example from previous posts that demonstrates how to meet your goals.
Let me know how it works on your end.

All the best,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Nedyalko Nikolov
Telerik team
answered on 27 Feb 2013, 07:35 AM
Hi,

Follow up:

I've forgotten to give you instructions how to use the example.
1. Hit "F5".
2. Press "Do something 1" button.
3. Press "Do something 2" button.

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex Martin
Top achievements
Rank 1
answered on 27 Feb 2013, 01:03 PM
Actually, I noticed that you are using the 2013.Q1 Telerik controls.  if I switch my test project to use the DLL in the libs folder of your sample it start working.  I'll upgrade my test project to use the new version of the DLL.

Tags
GridView
Asked by
Alex Martin
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Alex Martin
Top achievements
Rank 1
Share this question
or