Example #1
0
        /// <summary>
        /// Map items from the dictionary data to items of Type T.
        /// Each key in the data corresponds to a nested IDictionary which contains key/value
        /// pairs representing the properties/values of the type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static IList <T> MapCsv <T>(CsvDoc data, IErrors errors) where T : class, new()
        {
            var mapper  = new MapperCsv <T>();
            var results = mapper.Map(data, errors);

            return(results);
        }
        /// <summary>
        /// Determines if the time string specified is a time of day. e.g. 9am
        /// and within the bounds specified.
        /// </summary>
        /// <param name="time">Text to check.</param>
        /// <param name="errors">The error collection to populate with any errors.</param>
        /// <param name="tag">Tag used in an error message.</param>
        /// <returns>True if the text is a valid time.</returns>
        public static bool IsTimeOfDay(string time, IErrors errors, string tag)
        {
            TimeSpan span    = TimeSpan.MinValue;
            bool     isMatch = TimeSpan.TryParse(time, out span);

            return(CheckError(isMatch, errors, tag, _messages.TextInvalidTime));
        }
Example #3
0
        /// <summary>
        /// Map items from the dictionary data to items of Type T.
        /// Each key in the data corresponds to a nested IDictionary which contains key/value
        /// pairs representing the properties/values of the type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="path"></param>
        /// <param name="errors"></param>
        /// <param name="excludeProps"></param>
        /// <returns></returns>
        public static void MapToCsv <T>(IList <T> items, string path, IErrors errors, List <string> excludeProps) where T : class, new()
        {
            var mapper = new MapperCsv <T>();
            IDictionary <string, string> excludedPropsMap = excludeProps.ToDictionary();

            mapper.MapToFile(items, excludedPropsMap, path, errors);
        }
Example #4
0
        /// <summary>
        /// Maps all the keys/values in the data dictionary to the
        /// </summary>
        /// <param name="object">The object to map</param>
        /// <param name="data">The data to map to the object.</param>
        /// <param name="namefilter">Filter on the keys. e.g. "Location." will only map keys that contain "Location."</param>
        /// <param name="errors">Error list for collecting errors.</param>
        public static void MapTo(object obj, IDictionary data, string namefilter, IErrors errors)
        {
            // 1. Get all the public, instance, writable properties.
            Type type    = obj.GetType();
            var  propMap = ReflectionUtils.GetPropertiesAsMap(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);

            // 2. Making keys/filter case insensitive. Location. = location.
            namefilter = namefilter.Trim().ToLower();

            // 3. Iterate through all the keys.
            foreach (DictionaryEntry entry in data)
            {
                var keyname = entry.Key as string;
                keyname = keyname.Trim().ToLower();
                PropertyInfo prop = null;

                // 4. key in data matches filter?
                if (keyname.Contains(namefilter))
                {
                    // Get "City" from "Location.City";
                    string propname = keyname.Substring(keyname.IndexOf(".") + 1);
                    propname.Trim().ToLower();

                    // 5. propname exists in the data type?
                    if (propMap.ContainsKey(propname))
                    {
                        // 6. Finally map
                        prop = propMap[propname];
                        object val          = entry.Value;
                        object valConverted = ReflectionTypeChecker.ConvertToSameType(prop, val);
                        prop.SetValue(obj, valConverted, null);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Map items from the dictionary data to items of Type T.
        /// Each key in the data corresponds to a nested IDictionary which contains key/value
        /// pairs representing the properties/values of the type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static IList <T> Map <T>(IDictionary data, IErrors errors) where T : class, new()
        {
            var mapper  = new MapperIni <T>();
            var results = mapper.Map(data, errors);

            return(results);
        }
Example #6
0
        /// <summary>
        /// Map objects from the source and convert to list of type T. Collect errors into the IErrors.
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> MapFromFile(string filepath, IErrors errors)
        {
            IniDocument doc = new IniDocument(filepath, true);

            _data = doc;
            return(Map(errors));
        }
Example #7
0
        /// <summary>
        /// Map objects from the source and convert to list of type T. Collect errors into the IErrors.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> MapFromText(string content, IErrors errors)
        {
            IniDocument doc = new IniDocument(content, false);

            _data = doc;
            return(Map(errors));
        }
Example #8
0
        public static bool IsTimeOfDay(string time, IErrors errors, string errorKey)
        {
            TimeSpan minValue = TimeSpan.MinValue;
            bool     isValid  = TimeSpan.TryParse(time, out minValue);

            return(Validation.CheckError(isValid, errors, errorKey, "不是一个有效的时间."));
        }
        /// <summary>
        /// Determines if the time string specified is a time of day. e.g. 9am
        /// and within the bounds specified.
        /// </summary>
        /// <param name="time"></param>
        /// <param name="checkBounds"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool IsTimeOfDay(string time, IErrors errors, string tag)
        {
            TimeSpan span    = TimeSpan.MinValue;
            bool     isMatch = TimeSpan.TryParse(time, out span);

            return(CheckError(isMatch, errors, tag, "Text supplied is not a valid time."));
        }
Example #10
0
        /// <summary>
        /// Map objects from the source and convert to list of type T. Collect errors into the IErrors.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="data"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> MapFromText(string content, IErrors errors)
        {
            CsvDoc doc = new CsvDoc(content, false);

            _doc = doc;
            return(Map(errors));
        }
Example #11
0
        /// <summary>
        /// Map objects from the source and convert to list of type T. Collect errors into the IErrors.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="data"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> MapFromFile(string filepath, IErrors errors)
        {
            CsvDoc doc = new CsvDoc(filepath, true);

            _doc = doc;
            return(Map(errors));
        }
 /// <summary>
 /// Initialize the scaffold context.
 /// </summary>
 /// <param name="errors"></param>
 /// <param name="messages"></param>
 /// <param name="entityName"></param>
 /// <param name="parentControlId"></param>
 public ScaffoldContext(IValidationResults errors, IErrors messages, string entityName, string entityId, string parentControlId)
     : base(errors)
 {
     EntityName      = entityName;
     ParentControlId = parentControlId;
     EntityId        = entityId;
 }
 /// <summary>
 /// Add errors from entity to the model state.
 /// </summary>
 /// <param name="modelState"></param>
 /// <param name="entity"></param>
 public static void AddErrors(this ModelStateDictionary modelState, IErrors errors)
 {
     if (errors.Count > 0)
     {
         errors.EachFull(err => modelState.AddModelError("", err));
     }
 }
Example #14
0
        public static string JoinDetails(this IErrors source)
        {
            var errors       = source.Errors?.Select(e => e.Detail);
            var errorsString = errors == null ? "" : string.Join(',', errors);

            return(errorsString);
        }
Example #15
0
 public static void TransferMessages(IList <string> messages, IErrors errors)
 {
     foreach (string current in messages)
     {
         errors.Add(string.Empty, current);
     }
 }
Example #16
0
 /// <summary>
 /// Transfers all the messages from the source to the validation results.
 /// </summary>
 /// <param name="messages"></param>
 /// <param name="results"></param>
 public static void TransferMessages(IList<string> messages, IErrors errors)
 {
     foreach (string message in messages)
     {
         errors.Add(string.Empty, message);
     }
 }
Example #17
0
 /// <summary>
 /// Flash all the errors provided.
 /// </summary>
 /// <param name="errors"></param>
 public void FlashErrors(IErrors errors)
 {
     if (errors == null || errors.Count == 0)
     {
         return;
     }
     errors.EachFull(error => this.ModelState.AddModelError("", error));
 }
Example #18
0
 public static bool Validate(bool isError, IErrors errors, string message)
 {
     if (isError)
     {
         errors.Add(string.Empty, message);
     }
     return(!isError);
 }
 /// <summary>
 /// Check the condition and add the error.
 /// </summary>
 /// <param name="isValid"></param>
 /// <param name="errors"></param>
 /// <param name="tag"></param>
 /// <param name="error"></param>
 /// <param name="target"></param>
 /// <returns></returns>
 public static bool CheckError(bool isValid, IErrors errors, string tag, string error)
 {
     if (!isValid)
     {
         errors.Add(tag, error);
     }
     return isValid;
 }
 /// <summary>
 /// Check the condition and add the error.
 /// </summary>
 /// <param name="isValid"></param>
 /// <param name="errors"></param>
 /// <param name="tag"></param>
 /// <param name="error"></param>
 /// <param name="target"></param>
 /// <returns></returns>
 public static bool CheckError(bool isValid, IErrors errors, string tag, string error)
 {
     if (!isValid)
     {
         errors.Add(tag, error);                
     }
     return isValid;
 }
Example #21
0
 /// <summary>
 /// Validates the bool condition and adds the string error
 /// to the error list if the condition is invalid.
 /// </summary>
 /// <param name="isValid">Flag indicating if invalid.</param>
 /// <param name="error">Error message</param>
 /// <param name="results"><see cref="ValidationResults"/></param>
 /// <returns>True if isError is false, indicating no error.</returns>
 public static bool Validate(bool isError, IErrors errors, string message)
 {
     if (isError)
     {
         errors.Add(string.Empty, message);
     }
     return !isError;
 }
Example #22
0
 public static bool CheckError(bool isValid, IErrors errors, string errorKey, string error)
 {
     if (!isValid)
     {
         errors.Add(errorKey, error);
     }
     return(isValid);
 }
Example #23
0
 /// <summary>
 /// Initialize the entity action result.
 /// </summary>
 /// <param name="success"></param>
 /// <param name="message"></param>
 /// <param name="viewName"></param>
 /// <param name="errors"></param>
 /// <param name="item"></param>
 public EntityActionResult(bool success, string message = "", IErrors errors = null, object item = null, bool isAuthorized = true, bool isAvailable = true)
 {
     Success      = success;
     Message      = message;
     Errors       = errors;
     IsAuthorized = isAuthorized;
     IsAvailable  = isAvailable;
     Item         = item;
 }
Example #24
0
        public static void AddError(this IErrors target, Error error)
        {
            if (target.Errors == null)
            {
                target.Errors = new ErrorCollection();
            }

            target.Errors.Add(error);
        }
Example #25
0
        /// <summary>
        /// Map the objects.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> Map(object source, IErrors errors)
        {
            if (source == null || source.GetType() != typeof(CsvDoc))
            {
                return(new List <T>());
            }

            _doc = source as CsvDoc;
            return(Map(errors));
        }
Example #26
0
        /// <summary>
        /// Map the objects.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> Map(object source, IErrors errors)
        {
            if (source == null || !(source is IDictionary))
            {
                return(new List <T>());
            }

            _data = source as IDictionary;
            return(Map(errors));
        }
Example #27
0
        /// <summary>
        /// Map the objects.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> Map(object source, IErrors errors)
        {
            if (source == null || !(source is XmlDocument))
            {
                return(new List <T>());
            }

            _doc = source as XmlDocument;
            return(Map(errors));
        }
Example #28
0
        /// <summary>
        /// Maps the xml string to a list of items of type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contents"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static IList <T> MapXml <T>(string contents, IErrors errors = null) where T : class, new()
        {
            var mapper = new MapperXml <T>();
            var doc    = new XmlDocument();

            doc.LoadXml(contents);
            var results = mapper.Map(doc, errors);

            return(results);
        }
Example #29
0
        public void SetErrors(IErrors errors)
        {
#if CHECKERS
            if (errors == null || errors.Count == 0)
            {
                throw new ArgumentNullException("errors");
            }
#endif

            __ToolTip.SetToolTip(this, errors.GetErrorsSummary());
        }
Example #30
0
        public void SetErrors(IErrors errors)
        {
#if CHECKERS
            if (errors == null || errors.Count == 0)
            {
                throw new ArgumentNullException("errors");
            }
#endif

            __ToolTip.SetToolTip(this, errors.GetErrorsSummary());
        }
Example #31
0
        /// <summary>
        /// Map the objects.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="errors"></param>
        /// <param name="enableDynamicTypes"></param>
        /// <param name="dynamicTypeSectionKeyName"></param>
        /// <param name="dynamicTypeFactory"></param>
        /// <returns></returns>
        public IList <T> Map(object source, IErrors errors, bool enableDynamicTypes, string dynamicTypeSectionKeyName, Func <string, T> dynamicTypeFactory)
        {
            if (source == null || !(source is IDictionary))
            {
                return(new List <T>());
            }

            _data = source as IDictionary;
            _dynamicTypesEnabled = enableDynamicTypes;
            _dynamicTypeKeyName  = dynamicTypeSectionKeyName;
            _dynamicTypeFactory  = dynamicTypeFactory;
            return(Map(errors));
        }
        /// <summary>
        /// Internal method for handling errors.
        /// </summary>
        /// <param name="error"></param>
        /// <param name="exception"></param>
        /// <param name="handler"></param>
        /// <param name="errorResults"></param>
        /// <param name="arguments"></param>
        protected virtual void InternalHandle(object error, Exception exception, IErrors errors, object[] arguments)
        {
            string fullError = error == null ? string.Empty : error.ToString();

            // Add error to list and log.
            if (errors != null)
            {
                errors.Add(fullError);
                fullError = errors.Message();
            }

            Logger.Error(fullError, exception, arguments);
        }
        /// <summary>
        /// Validates the object using System.ComponentModel.DataAnnotations, and puts any errors into the errors object.
        /// </summary>
        /// <param name="objectToValidate">Object to validate</param>
        /// <param name="errors">The collection to put validation errors into.</param>
        /// <returns></returns>
        public static bool ValidateObject(object objectToValidate, IErrors errors = null)
        {

            var validationResults = new List<Val.ValidationResult>();
            var ctx = new Val.ValidationContext(objectToValidate, null, null);
            var isValid = Val.Validator.TryValidateObject(objectToValidate, ctx, validationResults, true);
            
            if(!isValid && errors != null)
                foreach (var result in validationResults)
                    errors.Add(result.ErrorMessage);
            
            return isValid;
        }
        public static ApiException Factory(IErrors errorDetails, IResponse errorResponse)
        {
            var httpResponse     = errorResponse.RawResponse;
            var exceptionMessage = string.Format("API Error Occured [{0} {1}]", ((int)httpResponse.StatusCode).ToString(), httpResponse.ReasonPhrase);

            exceptionMessage += errorDetails.Render();
            var exception = new ApiException(exceptionMessage)
            {
                Details  = errorDetails,
                Response = errorResponse
            };

            return(exception);
        }
        /// <summary>
        /// Check the text for the regex pattern and adds errors in incorrect.
        /// </summary>
        /// <param name="inputText"></param>
        /// <param name="allowNull"></param>
        /// <param name="regExPattern"></param>
        /// <param name="errors"></param>
        /// <param name="tag"></param>
        /// <param name="error"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool CheckErrorRegEx(string inputText, bool allowNull, string regExPattern, IErrors errors, string tag, string error)
        {
            bool isEmpty = string.IsNullOrEmpty(inputText);
            if (allowNull && isEmpty)
                return true;

            if (!allowNull && isEmpty)
            {
                errors.Add(tag, error);
                return false;
            }

            bool isValid = Regex.IsMatch(inputText, regExPattern);
            if (!isValid) errors.Add(tag, error);

            return isValid;
        }
 /// <summary>
 /// Determines if text supplied is numeric
 /// </summary>
 /// <param name="text"></param>
 /// <param name="checkMinBound"></param>
 /// <param name="checkMaxBound"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <returns></returns>
 public static bool IsNumeric(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.Numeric, errors, tag, "Text supplied is not a valid number.");
 }
 /// <summary>
 /// Determines if text is either lowercase/uppercase alphabets.
 /// </summary>
 /// <param name="text">The text check</param>
 /// <param name="allowNull">Whether or not the text can be null</param>
 /// <param name="errors">List of errors.</param>
 /// <param name="tag">Tag used to identify the error.</param>
 /// <returns></returns>
 public static bool IsAlpha(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.Alpha, errors, tag, "Text supplied must only contain chars " + RegexPatterns.Alpha);
 }
 /// <summary>
 /// Determines if email supplied is a valid zip with 4 additional chars.
 /// e.g. 12345-2321
 /// </summary>
 /// <param name="text">The text check</param>
 /// <param name="allowNull">Whether or not the text can be null</param>
 /// <param name="errors">List of errors.</param>
 /// <param name="tag">Tag used to identify the error.</param>
 /// <returns></returns>
 public static bool IsZipCodeWith4CharOptional(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.ZipCodeUSWithFourOptional, errors, tag, "Text supplied is not a valid US zipcode.");
 }
 /// <summary>
 /// Determines if ssn supplied is a valid ssn.
 /// </summary>
 /// <param name="ssn"></param>
 /// <returns></returns>
 public static bool IsSsn(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.SocialSecurity, errors, tag, "Text supplied is not a valid social security number.");
 }
 /// <summary>
 /// Determines if the phone number supplied is a valid US phone number.
 /// </summary>
 /// <param name="phone"></param>
 /// <returns></returns>
 public static bool IsPhoneUS(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.PhoneUS, errors, tag, "Text supplied is not a valid US phone number.");
 }
        /// <summary>
        /// Determines if the time string specified is a time of day. e.g. 9am
        /// and within the bounds specified.
        /// </summary>
        /// <param name="time"></param>
        /// <param name="checkBounds"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool IsTimeOfDayWithinRange(string time, bool checkMinBound, bool checkMaxBound, TimeSpan min, TimeSpan max, IErrors errors, string tag)
        {
            TimeSpan span = TimeSpan.MinValue;
            if (!TimeSpan.TryParse(time, out span))
                return CheckError(false, errors, tag, "Text supplied is not a valid time.");

            return IsTimeOfDayWithinRange(span, checkMinBound, checkMaxBound, min, max, errors, tag);
        }
Example #42
0
 public BoolErrorsItem(object item, bool success, string message, IErrors errors)
     : base(item, success, message)
 {
     _errors = errors;
 }
 /// <summary>
 /// Determines if string matches the regex.
 /// </summary>
 /// <param name="text">Text to match.</param>
 /// <param name="allowNull">Whether or not text can be null or empty for successful match.</param>
 /// <param name="regEx">Regular expression to use.</param>
 /// <returns>True if match, false otherwise.</returns>
 public static bool IsStringRegExMatch(string text, bool allowNull, string regEx, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, regEx, errors, tag, "Text supplied does not match expected pattern.");
 }
 /// <summary>
 /// Determines if the time string specified is a time of day. e.g. 9am
 /// and within the bounds specified.
 /// </summary>
 /// <param name="time"></param>
 /// <param name="checkBounds"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <returns></returns>
 public static bool IsTimeOfDay(string time, IErrors errors, string tag)
 {
     TimeSpan span = TimeSpan.MinValue;
     bool isMatch = TimeSpan.TryParse(time, out span);
     return CheckError(isMatch, errors, tag, "Text supplied is not a valid time.");
 }
        /// <summary>
        /// Determine if string is valid with regard to minimum / maximum length.
        /// </summary>
        /// <param name="text">Text to check length of.</param>
        /// <param name="allowNull">Indicate whether or not to allow null.</param>
        /// <param name="minLength">-1 to not check min length, > 0 to represent min length.</param>
        /// <param name="maxLength">-1 to not check max length, > 0 to represent max length.</param>
        /// <returns>True if match based on parameter conditions, false otherwise.</returns>
        public static bool IsStringLengthMatch(string text, bool allowNull, bool checkMinLength, bool checkMaxLength, int minLength, int maxLength, IErrors errors, string tag)
        {
            if (string.IsNullOrEmpty(text) && allowNull) return true;

            int textLength = text == null ? 0 : text.Length;

            // Check bounds . -1 value for min/max indicates not to check.
            if (checkMinLength && minLength > 0 && textLength < minLength)
                return CheckError(false, errors, tag, "Text supplied is less than min length(" + minLength + ")");

            if (checkMaxLength && maxLength > 0 && textLength > maxLength)
                return CheckError(false, errors, tag, "Text supplied is more than max length(" + maxLength + ")");

            return true;
        }
 /// <summary>
 /// Determines whether the text [is string in] [the specified values].
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="allowNull">if set to <c>true</c> [allow null].</param>
 /// <param name="compareCase">if set to <c>true</c> [compare case].</param>
 /// <param name="allowedValues">The allowed values.</param>
 /// <param name="errors">The errors.</param>
 /// <param name="tag">The tag.</param>
 /// <returns>
 /// 	<c>true</c> if [is string in] [the specified text]; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsStringIn(string text, bool allowNull, bool compareCase, string[] allowedValues, IErrors errors, string tag)
 {
     bool isEmpty = string.IsNullOrEmpty(text);
     if (isEmpty && allowNull) return true;
     if (isEmpty && !allowNull)
     {
         string vals = allowedValues.JoinDelimited(",", (val) => val);
         errors.Add(tag, "Text must be in : " + vals);
         return false;
     }
     bool isValid = false;
     foreach (string val in allowedValues)
     {
         if (string.Compare(text, val, compareCase) == 0)
         {
             isValid = true;
             break;
         }
     }
     if (!isValid)
     {
         string vals = allowedValues.JoinDelimited(",", (val) => val);
         errors.Add(tag, "Text must be in : " + vals);
         return false;
     }
     return true;
 }
 /// <summary>
 /// Determines if ssn supplied is a valid ssn.
 /// </summary>
 /// <param name="text">The text check</param>
 /// <param name="allowNull">Whether or not the text can be null</param>
 /// <param name="errors">List of errors.</param>
 /// <param name="tag">Tag used to identify the error.</param>
 /// <returns></returns>
 public static bool IsSsn(int ssn, IErrors errors, string tag)
 {
     return CheckErrorRegEx(ssn.ToString(), false, RegexPatterns.SocialSecurity, errors, tag, "Text supplied is not a valid social security number.");
 }
        /// <summary>
        /// Determines if the date supplied is a date within the specified bounds.
        /// </summary>
        /// <param name="date"></param>
        /// <param name="checkMinBound"></param>
        /// <param name="checkMaxBound"></param>
        /// <param name="minDate"></param>
        /// <param name="maxDate"></param>
        /// <returns></returns>
        public static bool IsDateWithinRange(DateTime date, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag)
        {
            if (checkMinBound && date.Date < minDate.Date)
            {
                errors.Add(tag, "Date supplied is less than minimum date " + minDate.ToShortDateString());
                return false;
            }
            if (checkMaxBound && date.Date > maxDate.Date)
            {
                errors.Add(tag, "Date supplied is more than maximum date " + maxDate.ToShortDateString());
                return false;
            }

            return true;
        }
 /// <summary>
 /// Flash all the errors provided.
 /// </summary>
 /// <param name="errors"></param>
 public void FlashErrors(IErrors errors)
 {
     if (errors == null || errors.Count == 0) return;
     errors.EachFull(error => this.ModelState.AddModelError("", error));
 }
 /// <summary>
 /// Determines if the date supplied is a date.
 /// </summary>
 /// <param name="text"></param>
 /// <param name="checkBounds"></param>
 /// <param name="minDate"></param>
 /// <param name="maxDate"></param>
 /// <returns></returns>
 public static bool IsDate(string text, IErrors errors, string tag)
 {
     DateTime result = DateTime.MinValue;
     return CheckError(DateTime.TryParse(text, out result), errors, tag, "Text supplied is not a valid date");
 }
Example #51
0
        /// <summary>
        /// Copies all errors from this instance over to the supplied instance.
        /// </summary>
        /// <param name="errors">The errors.</param>
        public void CopyTo(IErrors errors)
        {
            if (errors == null) return;

            if (_errorList != null)
                foreach (string error in _errorList)
                    errors.Add(error);

            if (_errorMap != null)
                foreach (KeyValuePair<string, string> pair in _errorMap)
                    errors.Add(pair.Key, pair.Value);
        }
 /// <summary>
 /// Determines if the time string specified is a time of day. e.g. 9am
 /// and within the bounds specified.
 /// </summary>
 /// <param name="time"></param>
 /// <param name="checkBounds"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <returns></returns>
 public static bool IsTimeOfDayWithinRange(TimeSpan time, bool checkMinBound, bool checkMaxBound, TimeSpan min, TimeSpan max, IErrors errors, string tag)
 {
     return true;
 }
 /// <summary>
 /// Determines if the phone number supplied is a valid US phone number.
 /// </summary>
 /// <param name="phone"></param>
 /// <returns></returns>
 public static bool IsPhoneUS(int phone, IErrors errors, string tag)
 {
     return CheckErrorRegEx(phone.ToString(), false, RegexPatterns.PhoneUS, errors, tag, "Text supplied is not a valid US phone number.");
 }
 /// <summary>
 /// Determines if url supplied is a valid url.
 /// </summary>
 /// <param name="text">The text check</param>
 /// <param name="allowNull">Whether or not the text can be null</param>
 /// <param name="errors">List of errors.</param>
 /// <param name="tag">Tag used to identify the error.</param>
 /// <returns></returns>
 public static bool IsUrl(string text, bool allowNull, IErrors errors, string tag)
 {
     return CheckErrorRegEx(text, allowNull, RegexPatterns.Url, errors, tag, "Text supplied is not a valid url.");
 }
        /// <summary>
        /// Determines if text supplied is numeric and within the min/max bounds.
        /// </summary>
        /// <param name="text">Text to check if it's numeric and within bounds.</param>
        /// <param name="checkMinBound">Whether or not to check</param>
        /// <param name="checkMaxBound"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool IsNumericWithinRange(string text, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag)
        {
            bool isNumeric = Regex.IsMatch(text, RegexPatterns.Numeric);
            if (!isNumeric)
            {
                errors.Add(tag, "Text supplied is not numeric.");
                return false;
            }

            double num = Double.Parse(text);
            return IsNumericWithinRange(num, checkMinBound, checkMaxBound, min, max, errors, tag);
        }
        /// <summary>
        /// Determines if the date supplied is a date.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="checkBounds"></param>
        /// <param name="minDate"></param>
        /// <param name="maxDate"></param>
        /// <returns></returns>
        public static bool IsDateWithinRange(string text, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag)
        {
            DateTime result = DateTime.MinValue;
            if (!DateTime.TryParse(text, out result))
            {
                errors.Add(tag, "Text supplied is not a valid date");
                return false;
            }

            return IsDateWithinRange(result, checkMinBound, checkMaxBound, minDate, maxDate, errors, tag);
        }
        /// <summary>
        /// Determines if text supplied is numeric and within the min/max bounds.
        /// </summary>
        /// <param name="text">Text to check if it's numeric and within bounds.</param>
        /// <param name="checkMinBound">Whether or not to check</param>
        /// <param name="checkMaxBound"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool IsNumericWithinRange(double num, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag)
        {
            if (checkMinBound && num < min)
            {
                errors.Add(tag, "Number supplied is less than " + min + ".");
                return false;
            }

            if (checkMaxBound && num > max)
            {
                errors.Add(tag, "Number supplied is more than " + max + ".");
                return false;
            }

            return true;
        }