Validation using Data Annotations

2 posts, 0 answers
  1. Craig
    Craig avatar
    113 posts
    Member since:
    Nov 2011

    Posted 27 Feb 2013 Link to this post

    I am using data annotations to validate items in a collection presented in a RadGridView.
    It successfully validates a property of an item in the collection e.g. a name is required
    // ...
        public class GroupOfItems
            [Required(ErrorMessage="Name must be provided!")]
            public string GroupName { get; set; }
    // ...

    I now want to ensure the name is unique within the collection. I have tried adding the [Key] attribute but no error is indicated if I enter another item with the same name. It may be that [Key] is not appropriate in this context.

    What can you suggest I do to implement this validation rule?
    PS. I'm using C#, WPF, MVVM, Rad Controls for WPF 2012.3.1129.40

  2. Nedyalko Nikolov
    Nedyalko Nikolov avatar
    871 posts

    Posted 28 Feb 2013 Link to this post


    There is no built-in "UniqueAttribute" in DataAnnotation assembly, such attribute cannot be general (it depends from user collection). You could write your own just create an attribute class which inherits from DataAnnotation.ValidationAttribute abstract class.

    Kind regards,
    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top