DataAnnotation Validation Error on not shown properties

9 posts, 0 answers
  1. Alex Martin
    Alex Martin avatar
    11 posts
    Member since:
    May 2010

    Posted 20 Feb 2013 Link to this post

    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.
  2. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 21 Feb 2013 Link to this post

    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.

  3. Alex Martin
    Alex Martin avatar
    11 posts
    Member since:
    May 2010

    Posted 21 Feb 2013 Link to this post

    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.
  4. Alex Martin
    Alex Martin avatar
    11 posts
    Member since:
    May 2010

    Posted 26 Feb 2013 Link to this post

    Anyone has suggestion?
  5. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 26 Feb 2013 Link to this post

    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.

  6. Alex Martin
    Alex Martin avatar
    11 posts
    Member since:
    May 2010

    Posted 26 Feb 2013 Link to this post

    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;
    }

  7. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 27 Feb 2013 Link to this post

    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.

  8. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 27 Feb 2013 Link to this post

    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.

  9. Alex Martin
    Alex Martin avatar
    11 posts
    Member since:
    May 2010

    Posted 27 Feb 2013 Link to this post

    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.

Back to Top