private bool ValueIsValid(object value) { // Go through all the model validation attribute to make sure they're valid foreach (var attrib in Column.Attributes.Cast <Attribute>().OfType <ValidationAttribute>()) { // Ignore it if it's found in the ignore list if (_ignoredModelValidationAttributes != null && _ignoredModelValidationAttributes.ContainsKey(attrib.GetType())) { continue; } //DynamicValidator can not pass in a ValidationContext as it does //not have an easy way to get the data object row. Hence we will //not support attributes that require Validation Context (Ex : CompareAttribute). if (attrib.RequiresValidationContext) { continue; } if (!attrib.IsValid(value)) { ErrorMessage = HttpUtility.HtmlEncode(StringLocalizerUtil.GetLocalizedString(attrib, Column.DisplayName)); return(false); } } return(true); }
private void SetUpRangeValidator(RangeValidator validator, MetaColumn column) { // Nothing to do if no range was specified var rangeAttribute = column.Attributes.OfType <RangeAttribute>().FirstOrDefault(); if (rangeAttribute == null) { return; } // Make sure the attribute doesn't get validated a second time by the DynamicValidator IgnoreModelValidationAttribute(rangeAttribute.GetType()); validator.Enabled = true; Func <object, string> converter; switch (validator.Type) { case ValidationDataType.Integer: converter = val => Convert.ToInt32(val, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture); break; case ValidationDataType.Double: converter = val => Convert.ToDouble(val, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture); break; case ValidationDataType.String: default: converter = val => val.ToString(); break; } validator.MinimumValue = converter(rangeAttribute.Minimum); validator.MaximumValue = converter(rangeAttribute.Maximum); if (String.IsNullOrEmpty(validator.ErrorMessage)) { validator.ErrorMessage = HttpUtility.HtmlEncode( StringLocalizerUtil.GetLocalizedString(rangeAttribute, column.DisplayName)); } }
private void SetUpRegexValidator(RegularExpressionValidator validator, MetaColumn column) { // Nothing to do if no regex was specified var regexAttribute = column.Attributes.OfType <RegularExpressionAttribute>().FirstOrDefault(); if (regexAttribute == null) { return; } // Make sure the attribute doesn't get validated a second time by the DynamicValidator IgnoreModelValidationAttribute(regexAttribute.GetType()); validator.Enabled = true; validator.ValidationExpression = regexAttribute.Pattern; if (String.IsNullOrEmpty(validator.ErrorMessage)) { validator.ErrorMessage = HttpUtility.HtmlEncode( StringLocalizerUtil.GetLocalizedString(regexAttribute, column.DisplayName)); } }