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)); }
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); }
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(); } }
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; }
public virtual void Validate(ValidationResult validationResults) { }
protected override void Before_each_spec() { _validationResult = new ValidationResult(); }
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; } } }
public void SetNotification(ValidationResult validationResult) { }
protected abstract void Validate(object target, object rawValue, ValidationResult validationResult);
protected void AddMessage(ValidationResult validationResult, string message) { validationResult.AddError(Property.Name, !string.IsNullOrEmpty(OptionalMessage) ? OptionalMessage : message); }
public void Validate(object target, ValidationResult validationResult) { object rawValue = _property.GetValue(target, null); Validate(target, rawValue, validationResult); }
protected override void Validate(object target, object rawValue, ValidationResult validationResult) { if (rawValue == null) AddMessage(validationResult, "Field is required."); }