Ejemplo n.º 1
0
        /// <summary>
        /// Validates the specified property.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="runner">The runner.</param>
        /// <param name="propertyExpression">The property expression.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <returns></returns>
        public static IEnumerable <ValidationResult> Validate <TModel, TProperty>(
            this IValidationRunner runner, Expression <Func <TModel, TProperty> > propertyExpression, TProperty propertyValue)
        {
            MemberExpression memberExpression = (MemberExpression)propertyExpression.Body;

            return(runner.Validate(typeof(TModel), propertyValue, memberExpression.Member.Name));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.
        /// </summary>
        /// <returns>
        /// true if the value in the input control is valid; otherwise, false.
        /// </returns>
        protected override bool EvaluateIsValid()
        {
            Type   modelType = GetModelType();
            object value;

            try
            {
                value = GetModelPropertyValue(PropertyName, ControlToValidate);
            }
            catch (ValidationException ex)
            {
                ErrorMessage = ex.ValidationResult.ErrorMessage;
                return(false);
            }

            IEnumerable <ValidationResult> errors =
                _validationRunner.Validate(modelType, value, PropertyName);

            if (errors.Any())
            {
                ErrorMessage = errors.GetErrorMessage();
                return(false);
            }

            return(true);
        }
        public void Booking_GetErrors_ClientName_Null()
        {
            Booking instance = new Booking
            {
                ArrivalDate    = new DateTime(2009, 8, 13),
                NumberOfGuests = 2
            };

            IEnumerable <ValidationResult> errors            = _validationRunner.Validate(instance);
            List <ValidationResult>        validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(instance, new ValidationContext(instance, null, null), validationResults);

            Assert.That(errors.Count(), Is.EqualTo(1));
            Assert.That(validationResults.Count, Is.EqualTo(1));

            ValidationResult errorInfo = errors.First();

            Assert.That(errorInfo.MemberNames.Contains("ClientName"));
            Assert.That(errorInfo.ErrorMessage, Is.EqualTo(Booking.ClientNameRequiredMessage));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Validates the specified model.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="runner">The runner.</param>
 /// <param name="model">The model.</param>
 /// <returns></returns>
 public static IEnumerable <ValidationResult> Validate <T>(this IValidationRunner runner, T model)
 {
     return(runner.Validate(model));
 }