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

DataAnnotation derived ValidationAttribute

3 Answers 94 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 19 Feb 2013, 07:30 PM
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;
            }
         
        }

3 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 20 Feb 2013, 12:07 PM
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.

0
Alex Martin
Top achievements
Rank 1
answered on 20 Feb 2013, 01:55 PM
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");
}
}
}



0
Nedyalko Nikolov
Telerik team
answered on 21 Feb 2013, 02:06 PM
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.

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