CustomValidation attribute

Nov 24, 2011 at 1:46 PM

Geert,

When I define a CustomValidation attribute to my model (on the server side of my RIA service, but this gets copied to the client side, so calls are local) I notice that the custom validator does not work (I don't see its error message). It is possible that Catel does not support this (yet). (If this is the case, I can do the validation in the override of ValidateFields.).

However I also notice that adding characters in the editbox slows down noticably after I delete all characters from the editbox and then start adding characters again (I am not sure this is something for you to fix. I just want to report this here).

The bindings that I use are like this, and I have also included the Model code and validator:

"{Binding Name, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"

(

    public partial class Person
    {
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        public Address Address { get; set; }
    }

    [MetadataTypeAttribute(typeof(Person.PersonMetadata))]
    public partial class Person
    {
        internal sealed class PersonMetadata
        {
            // Metadata classes are not meant to be instantiated.
            private PersonMetadata()
            {
            }

            [StringLength(20, MinimumLength = 10)]
            [CustomValidation(typeof(PersonValidator), "NameStartsWithD")]
            public string Name { get; set; }
        }
    }

 

    public class PersonValidator
    {
        
        public static ValidationResult NameStartsWithD(string name, ValidationContext context)
        {
            if ((name != null))
            {

                if (!name.StartsWith("D"))
                {
                    return new ValidationResult("Name has to start with D");
                }
            }
            return ValidationResult.Success;
        }
    }

 


Nov 24, 2011 at 4:46 PM

When you add new characters, it does not update any bindings immediately unless you use the UpdateBindingOnTextChanged behavior. If that is not the case, the delay is not caused by Catel since there is no binding intrusion. Try doing the same in chrome and check if the problem exists there as well.

Ok, up to the actual problem: the custom validator. Are you sure it fires on the client? Try opening the generated RIA services client and make sure that the client side code is also decorated with the custom validator (I have seen it fail more than once). If that is the case, I will investigate this further.

Nov 24, 2011 at 5:57 PM

Thanks for the hints. Found the problem(s):

The custom validator was called, but the ValidationResult creating and returning needs a second parameter with the name(s) of the properties to which it applies.

The performance issue was caused by the DeveloperExpress TextEdit control I am using. It has a ValidateOnTextInput property that default True. This causes the custom validator to be called for each character that is added/removed. The fact that it only slows down after deleting all characters and then adding characters agains is probably something that I need to report to DevExpress.

Sorry for falsely accusing Catel :-)