Exemple #1
0
        /// <summary>
        /// Evalutes the user input for field.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="property">The property.</param>
        /// <param name="form">The form.</param>
        /// <returns></returns>
        private static FieldValidationSummary EvaluteUserInputForField(PropertyInfo property, T form)
        {
            FieldValidationSummary    result       = null;
            List <IValidateAttribute> fieldCritera = GetValidationCriteraForProperty(property);

            if (fieldCritera.Count > 0)
            {
                object val           = property.GetValue(form, null);
                string propertyValue = Convert.ToString(val);
                result = ValidateField(property, fieldCritera, propertyValue);
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Validates the specified form.
        /// </summary>
        /// <returns>
        ///     <c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid()
        {
            ValidationResults = new List <FieldValidationSummary>();
            PropertyInfo[] properties = _form.GetType().GetProperties();

            for (int index = 0; index < properties.Length; index++)
            {
                FieldValidationSummary result = EvaluteUserInputForField(properties[index], _form);

                if (result != null && result.Failed.Count > 0)
                {
                    ValidationResults.Add(result);
                }
            }

            return(ValidationResults.Count == 0);
        }
Exemple #3
0
        /// <summary>
        /// Validates the field.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="fieldCritera">The field critera.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <returns></returns>
        private static FieldValidationSummary ValidateField(PropertyInfo property, IList <IValidateAttribute> fieldCritera, string propertyValue)
        {
            FieldValidationSummary result = new FieldValidationSummary {
                FieldName = property.Name
            };

            for (int indx = 0; indx < fieldCritera.Count; indx++)
            {
                if (!fieldCritera[indx].IsValid(propertyValue))
                {
                    FailedValidation validation = new FailedValidation {
                        ErrorMessage = fieldCritera[indx].ErrorMessage, InputValue = propertyValue, FriendlyName = (string.IsNullOrEmpty(fieldCritera[indx].FriendlyName)? property.Name : fieldCritera[indx].FriendlyName)
                    };
                    result.Failed.Add(validation);
                }
            }

            return(result);
        }