Example #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (validationContext == null)
            {
                throw new ArgumentException("Validation context is required");
            }

            PropertyInfo realProperty = validationContext.ObjectType.GetProperty(_propertyName);

            if (realProperty == null)
            {
                throw new ArgumentException("Property named {0} doesn`t exist.", _propertyName);
            }

            object realPropertyValue = realProperty.GetValue(validationContext.ObjectInstance);

            bool isPropertysEqual = Object.Equals(realPropertyValue, _expectedPropertyValue);

            if (isPropertysEqual)
            {
                if (value == null)
                {
                    if (ErrorMessageResourceType != null && !String.IsNullOrWhiteSpace(ErrorMessageResourceName))
                    {
                        string message = ResourceHelpers.GetResourceValue(ErrorMessageResourceType, ErrorMessageResourceName);
                        return(new ValidationResult(message));
                    }
                    else if (!String.IsNullOrWhiteSpace(ErrorMessage))
                    {
                        return(new ValidationResult(ErrorMessage));
                    }
                    else
                    {
                        throw new ArgumentException("Jeden z następujących zestawów właściwości musi spełniać następujące kryteria:\n" +
                                                    " 1) ErrorMessageResourceType (różny od null), ErrorMessageResourceName (różny od null, nie składający się tylko z białych znaków, nie będący pustym stringiem )\n" +
                                                    " 2) ErrorMessage (różny od null, nie składający się tylko z białych znaków, nie będący pustym stringiem )");
                    }
                }
            }
            return(ValidationResult.Success);
        }