예제 #1
0
        /// <summary>
        /// Gets the validation rules.
        /// </summary>
        public virtual IEnumerable <ViewModelPropertyValidationRule> TranslateValidationRules(PropertyInfo property, IEnumerable <ValidationAttribute> validationAttributes)
        {
            foreach (var attribute in validationAttributes)
            {
                var validationRule = new ViewModelPropertyValidationRule(sourceValidationAttribute: attribute, staticPropertyName: property.Name);
                // TODO: extensibility

                var displayAttribute = property.GetCustomAttribute <DisplayAttribute>();
                if (displayAttribute != null)
                {
                    validationRule.PropertyNameResolver = () => displayAttribute.GetName();
                }

                switch (attribute)
                {
                case RequiredAttribute _:
                    validationRule.ClientRuleName = "required";
                    break;

                case RegularExpressionAttribute regularExpressionAttr:
                    validationRule.ClientRuleName = "regularExpression";
                    validationRule.Parameters     = new[] { regularExpressionAttr.Pattern };
                    break;

                case RangeAttribute rangeAttr:
                    validationRule.ClientRuleName = "range";
                    validationRule.Parameters     = new[] { rangeAttr.Minimum, rangeAttr.Maximum };
                    break;

                case DotvvmEnforceClientFormatAttribute enforceClientFormatAttr:
                    validationRule.ClientRuleName = "enforceClientFormat";
                    validationRule.Parameters     = new object[] { enforceClientFormatAttr.AllowNull, enforceClientFormatAttr.AllowEmptyString,
                                                                   enforceClientFormatAttr.AllowEmptyStringOrWhitespaces };
                    break;

                case EmailAddressAttribute _:
                    validationRule.ClientRuleName = "emailAddress";
                    break;

                default:
                    validationRule.ClientRuleName = string.Empty;
                    break;
                }

                yield return(validationRule);
            }
        }
        /// <summary>
        /// Gets the validation rules.
        /// </summary>
        public virtual IEnumerable <ViewModelPropertyValidationRule> TranslateValidationRules(PropertyInfo property, IEnumerable <ValidationAttribute> validationAttributes)
        {
            foreach (var attribute in validationAttributes)
            {
                var validationRule = new ViewModelPropertyValidationRule(sourceValidationAttribute: attribute, staticPropertyName: property.Name);
                // TODO: extensibility

                var displayAttribute = property.GetCustomAttribute <DisplayAttribute>();
                if (displayAttribute != null)
                {
                    validationRule.PropertyNameResolver = () => displayAttribute.GetName();
                }

                if (attribute is RequiredAttribute)
                {
                    validationRule.ClientRuleName = "required";
                }
                else if (attribute is RegularExpressionAttribute)
                {
                    var typedAttribute = (RegularExpressionAttribute)attribute;

                    validationRule.ClientRuleName = "regularExpression";
                    validationRule.Parameters     = new[] { typedAttribute.Pattern };
                }
                else if (attribute is RangeAttribute)
                {
                    var typed = (RangeAttribute)attribute;

                    validationRule.ClientRuleName = "range";
                    validationRule.Parameters     = new[] { typed.Minimum, typed.Maximum };
                }
                else if (attribute is DotvvmEnforceClientFormatAttribute)
                {
                    var typed = (DotvvmEnforceClientFormatAttribute)attribute;

                    validationRule.ClientRuleName = "enforceClientFormat";
                    validationRule.Parameters     = new object[] { typed.AllowNull, typed.AllowEmptyString, typed.AllowEmptyStringOrWhitespaces };
                }
                else
                {
                    validationRule.ClientRuleName = string.Empty;
                }

                yield return(validationRule);
            }
        }
예제 #3
0
        public virtual IEnumerable <ViewModelPropertyValidationRule> TranslateValidationRules(PropertyInfo property, IEnumerable <ValidationAttribute> validationAttributes)
        {
            var addEnforceClientFormat = true;

            foreach (var attribute in validationAttributes)
            {
                var validationRule = new ViewModelPropertyValidationRule(sourceValidationAttribute: attribute, staticPropertyName: property.Name);
                // TODO: extensibility

                var displayAttribute = property.GetCustomAttribute <DisplayAttribute>();
                if (displayAttribute != null)
                {
                    validationRule.PropertyNameResolver = () => displayAttribute.GetName();
                }

                switch (attribute)
                {
                case RequiredAttribute _:
                    validationRule.ClientRuleName = "required";
                    break;

                case RegularExpressionAttribute regularExpressionAttr:
                    validationRule.ClientRuleName = "regularExpression";
                    validationRule.Parameters     = new[] { regularExpressionAttr.Pattern };
                    break;

                case RangeAttribute rangeAttr:
                    validationRule.ClientRuleName = "range";
                    validationRule.Parameters     = new[] { rangeAttr.Minimum, rangeAttr.Maximum };
                    break;

                case DotvvmClientFormatAttribute enforceClientFormatAttr:
                    addEnforceClientFormat = false;
                    if (enforceClientFormatAttr.Disable)
                    {
                        break;
                    }

                    validationRule.ClientRuleName = "enforceClientFormat";
                    validationRule.Parameters     = new object[] {
                        enforceClientFormatAttr.AllowNull,
                        enforceClientFormatAttr.AllowEmptyString,
                        enforceClientFormatAttr.AllowEmptyStringOrWhitespaces
                    };
                    break;

                case EmailAddressAttribute _:
                    validationRule.ClientRuleName = "emailAddress";
                    break;

                default:
                    validationRule.ClientRuleName = string.Empty;
                    break;
                }

                yield return(validationRule);
            }
            // enforce client format by default
            if (addEnforceClientFormat && (property.PropertyType.IsNullable() && property.PropertyType.UnwrapNullableType().IsNumericType() || property.PropertyType.UnwrapNullableType().IsDateOrTimeType()))
            {
                var enforceClientFormatAttr = new DotvvmClientFormatAttribute();

                var validationRule =
                    new ViewModelPropertyValidationRule(sourceValidationAttribute: enforceClientFormatAttr,
                                                        staticPropertyName: property.Name)
                {
                    Parameters = new object[] {
                        enforceClientFormatAttr.AllowNull,
                        enforceClientFormatAttr.AllowEmptyString,
                        enforceClientFormatAttr.AllowEmptyStringOrWhitespaces
                    },
                    ClientRuleName = "enforceClientFormat"
                };

                yield return(validationRule);
            }
        }