Exemplo n.º 1
0
        protected override void Validate(object target, object rawValue, ValidationResult validationResult)
        {
            if (!(rawValue is string)) return;

             if (rawValue.ToString().Length < _minimumLength)
            AddMessage(validationResult, string.Format("Must be {0} or more characters.", _minimumLength));
        }
Exemplo n.º 2
0
        protected override void Validate(object target, object rawValue, ValidationResult validationResult)
        {
            string stringValue = rawValue as string;

             Regex charRegex = new Regex(_pattern);

             if (!charRegex.IsMatch(stringValue))
            AddMessage(validationResult, ERROR_MESSAGE);
        }
Exemplo n.º 3
0
        private void DisplayErrorNotifications(ValidationResult validationResult, Dictionary<string, IValidationAware> validationObjects)
        {
            var messages = new List<ValidatonError>(validationResult.Errors);

             foreach (KeyValuePair<string, IValidationAware> validationObject in validationObjects)
             {
            ValidatonError message = messages.Find(match => match.Property == validationObject.Key);

            if (message != null)
               validationObject.Value.ShowError(message.Message);
            else
               validationObject.Value.ClearError();
             }
        }
Exemplo n.º 4
0
        public ValidationResult Validate(object[] targets, Dictionary<string, IValidationAware> validationObjects)
        {
            var result = new ValidationResult();

             foreach (object target in targets)
             {
            Type targetType = target.GetType();
            IList<ValidationAttribute> validations = ScanTypeForValidationAttributes(targetType);

            foreach (ValidationAttribute attribute in validations)
               attribute.Validate(target, result);

            if (target is ISelfValidator)
            {
               ((ISelfValidator)target).Validate(result);
            }
             }

             DisplayErrorNotifications(result, validationObjects);

             return result;
        }
Exemplo n.º 5
0
 public virtual void Validate(ValidationResult validationResults)
 {
 }
Exemplo n.º 6
0
 protected override void Before_each_spec()
 {
     _validationResult = new ValidationResult();
 }
Exemplo n.º 7
0
        protected override void Validate(object target, object rawValue, ValidationResult validationResult)
        {
            if (rawValue == null)
             {
            AddMessage(validationResult, "Value is null.");
            return;
             }

             if (rawValue.GetType() == typeof (string))
             {
            var theValue = (string)rawValue;
            if (string.IsNullOrEmpty(theValue))
            {
               AddMessage(validationResult, "String is empty.");
               return;
            }
             }

             if (rawValue.GetType() == typeof (Guid))
             {
            var theValue = (Guid)rawValue;
            if (theValue == Guid.Empty)
            {
               AddMessage(validationResult, "Identifier is empty.");
               return;
            }
             }

             if (rawValue is ICollection)
             {
            var theValue = rawValue as ICollection;
            if (theValue.Count == 0)
            {
               AddMessage(validationResult, "Collection is empty.");
               return;
            }
             }

             if (rawValue.GetType() == typeof(decimal))
             {
            var theValue = (decimal) rawValue;
            if (theValue == 0.0M)
            {
               AddMessage(validationResult,"Value is zero.");
               return;
            }
             }

             if (rawValue.GetType() == typeof(int)) {
            var theValue = (int)rawValue;
            if (theValue == 0) {
               AddMessage(validationResult, "Value is zero.");
               return;
            }
             }

             if (rawValue.GetType() == typeof(DateTime))
             {
            var theValue = (DateTime)rawValue;
            if (theValue == DateTime.MaxValue ||
                theValue == DateTime.MinValue) {
               AddMessage(validationResult, "Value is not set.");
               return;
            }
             }
        }
Exemplo n.º 8
0
 public void SetNotification(ValidationResult validationResult)
 {
 }
Exemplo n.º 9
0
 protected abstract void Validate(object target, object rawValue, ValidationResult validationResult);
Exemplo n.º 10
0
 protected void AddMessage(ValidationResult validationResult, string message)
 {
     validationResult.AddError(Property.Name, !string.IsNullOrEmpty(OptionalMessage) ? OptionalMessage : message);
 }
Exemplo n.º 11
0
 public void Validate(object target, ValidationResult validationResult)
 {
     object rawValue = _property.GetValue(target, null);
      Validate(target, rawValue, validationResult);
 }
Exemplo n.º 12
0
 protected override void Validate(object target, object rawValue, ValidationResult validationResult)
 {
     if (rawValue == null)
     AddMessage(validationResult, "Field is required.");
 }