DataAnnotation derived ValidationAttribute

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

    Posted 19 Feb 2013 Link to this post

    I'm using DataAnnotations for validation.  Built-in MS annotation such as [RequiredAttribute] works fine but custom annotation are firing and validating properly. However, the cell aren't getting the red border, only the error message tooltip. Any idea why?


     [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
            public class MustBeUniquePostalCodeAttribute : ValidationAttribute
            {

                private bool v;

                protected override ValidationResult IsValid(object value, ValidationContext validationContext)
                {
                    DcfItemViewModel instance = validationContext.ObjectInstance as DcfItemViewModel;

                    v = true;
                    if (instance.isNew)
                    {
                        string postalCode = instance.PostalCode;
                        IList<Dcf> dcfList = instance.Parent as IList<Dcf>;
                        Dcf dcfEntity = instance.dcfEntity;


                        bool exists = dcfList.Any(d => !d.Equals(dcfEntity) && String.Equals(postalCode, d.PostalCode, StringComparison.OrdinalIgnoreCase));

                        if (exists)
                        {
                            v = false;
                            return new ValidationResult("Value must be unique");
                        }
                    }
         
                    return ValidationResult.Success;
            
                }

                public override bool IsValid(object value)
                {
                    return v;
                }
             
            }
  2. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 20 Feb 2013 Link to this post

    Hello,

    I'm attaching my sample project that demonstrates how to achieve the desired behavior (according to me everything works as expected).
    Could you please give it a try and let me know about the result?

    Kind regards,
    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 20 Feb 2013 Link to this post

    Just tested your project and indeed it work in your sample.  After further testing, I found the issue may be when you have more than one DataAnnotation on the same property.  If you modify your project to add a [Required] to the Club.Name you will see that the red border does no show in edit more anymore.

            [Required]
    [MyCustomValidation]
    public string Name
    {
    get
    {
    return this.name;
    }
    set
    {
    if (value != this.name)
    {
    this.name = value;
    this.OnPropertyChanged("Name");
    }
    }
    }



  4. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 21 Feb 2013 Link to this post

    Hello,

    In fact the red border is shown at the second attempt to commit the cell, but indeed first commit does not draw a red border. The problem comes from the fact that "VisualStates" for the editor presenter control have equal names with System.Windows.Controls.Control visual states (the problem state is the default one which name is "Valid"). For some reason .NET framework changes state of the control to valid (maybe because there is no binding errors) and since our control has "Valid" states it is applied. We will fix this problem by changing the default visual state name to "Normal" and the problem will be solved with the next internal build (Monday 25 Feb).

    I've added 1000 Telerik points to your account accordingly.

    All the best,
    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top