예제 #1
0
        /// <summary>
        /// Adds an error to the specification result.
        /// </summary>
        /// <param name="error">The error.</param>
        public void AddError(DomainSpecificationError error)
        {
            ExceptionHelper.ThrowIfNull("error", error);

            if (!m_errors.Contains(error))
            {
                m_errors.Add(error);
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the error from invalid attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns></returns>
        protected virtual DomainSpecificationError GetErrorFromInvalidAttribute(ValidationAttribute attribute, string fieldName)
        {
            string errorMessage = string.IsNullOrEmpty(attribute.ErrorMessage) ? null : attribute.ErrorMessage;

            DomainSpecificationError currentError = null;

            if (m_errorReasons.TryGetValue(attribute.GetType(), out currentError))
            {
                currentError = new DomainSpecificationError(currentError.ErrorCode ?? -1,
                                                            errorMessage ?? currentError.NotSatisfiedReason,
                                                            fieldName);

                try
                {
                    if (attribute is MinLengthAttribute)
                    {
                        var minLengthAttr = attribute as MinLengthAttribute;
                        currentError.NotSatisfiedReason = string.Format(
                            currentError.NotSatisfiedReason,
                            minLengthAttr.Length);
                    }
                    else if (attribute is MaxLengthAttribute)
                    {
                        var maxLengthAttr = attribute as MaxLengthAttribute;
                        currentError.NotSatisfiedReason = string.Format(
                            currentError.NotSatisfiedReason,
                            maxLengthAttr.Length);
                    }
                }
                catch (FormatException)
                {
                }
            }
            else
            {
                currentError = new DomainSpecificationError(-1, errorMessage, fieldName);
            }

            return(currentError);
        }