/// <summary>
        ///     Retrieves a collection of client validation rules (which are next sent to browsers).
        /// </summary>
        /// <returns>
        ///     A collection of client validation rules.
        /// </returns>
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage   = FormattedErrorMessage,
                ValidationType = ProvideUniqueValidationType("requiredif")
            };

            rule.ValidationParameters.Add("expression", Expression);
            rule.ValidationParameters.Add("fieldsmap", FieldsMap.ToJson());
            rule.ValidationParameters.Add("constsmap", ConstsMap.ToJson());
            rule.ValidationParameters.Add("allowempty", AllowEmpty.ToJson());
            yield return(rule);
        }
示例#2
0
        /// <summary>
        ///     Generates client validation rule with the basic set of parameters.
        /// </summary>
        /// <param name="type">The validation type.</param>
        /// <returns>
        ///     Client validation rule with the basic set of parameters.
        /// </returns>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException"></exception>
        protected ModelClientValidationRule GetBasicRule(string type)
        {
            try
            {
                var rule = new ModelClientValidationRule
                {
                    ErrorMessage   = FormattedErrorMessage,
                    ValidationType = ProvideUniqueValidationType(type)
                };

                rule.ValidationParameters.Add("expression", Expression.ToJson());

                Debug.Assert(FieldsMap != null);
                if (FieldsMap.Any())
                {
                    rule.ValidationParameters.Add("fieldsmap", FieldsMap.ToJson());
                }
                Debug.Assert(ConstsMap != null);
                if (ConstsMap.Any())
                {
                    rule.ValidationParameters.Add("constsmap", ConstsMap.ToJson());
                }
                Debug.Assert(EnumsMap != null);
                if (EnumsMap.Any())
                {
                    rule.ValidationParameters.Add("enumsmap", EnumsMap.ToJson());
                }
                Debug.Assert(ParsersMap != null);
                if (ParsersMap.Any())
                {
                    rule.ValidationParameters.Add("parsersmap", ParsersMap.ToJson());
                }
                Debug.Assert(ErrFieldsMap != null);
                if (ErrFieldsMap.Any())
                {
                    rule.ValidationParameters.Add("errfieldsmap", ErrFieldsMap.ToJson());
                }

                return(rule);
            }
            catch (Exception e)
            {
                throw new ValidationException(
                          $"{GetType().Name}: collecting of client validation rules for {FieldName} field failed.", e);
            }
        }
示例#3
0
        /// <summary>
        /// Retrieves a collection of client validation rules (rules sent to browsers).
        /// </summary>
        /// <returns>
        /// A collection of client validation rules.
        /// </returns>
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            var count = Storage.Get <int>(AnnotatedField) + 1;

            Assert.AttribsQuantityAllowed(count);

            Storage.Set(AnnotatedField, count);

            var suffix = count == 1 ? string.Empty : char.ConvertFromUtf32(95 + count);
            var rule   = new ModelClientValidationRule
            {
                ErrorMessage   = FormattedErrorMessage,
                ValidationType = string.Format("assertthat{0}", suffix)
            };

            rule.ValidationParameters.Add("expression", Expression);
            rule.ValidationParameters.Add("fieldsmap", FieldsMap.ToJson());
            rule.ValidationParameters.Add("constsmap", ConstsMap.ToJson());
            yield return(rule);
        }
示例#4
0
        /// <summary>
        ///     Attaches client validation rule to the context.
        /// </summary>
        /// <param name="context">The Client Model Validation Context.</param>
        /// <param name="type">The validation type.</param>
        /// <param name="defaultErrorMessage">The default Error Message.</param>
        /// <returns>
        ///     void
        /// </returns>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException"></exception>
        protected void AttachValidationRules(ClientModelValidationContext context, string type, string defaultErrorMessage)
        {
            var errorMessage         = !string.IsNullOrEmpty(FormattedErrorMessage) ? FormattedErrorMessage : defaultErrorMessage;
            var uniqueValidationType = ProvideUniqueValidationType(type);

            try
            {
                MergeAttribute(context.Attributes, "data-val", "true");
                MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}", errorMessage);
                MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-expression", Expression.ToJson());
                Debug.Assert(FieldsMap != null);
                if (FieldsMap.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-fieldsmap", FieldsMap.ToJson());
                }
                Debug.Assert(ConstsMap != null);
                if (ConstsMap.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-constsmap", ConstsMap.ToJson());
                }
                Debug.Assert(EnumsMap != null);
                if (EnumsMap.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-enumsmap", EnumsMap.ToJson());
                }
                Debug.Assert(MethodsList != null);
                if (MethodsList.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-methodslist", MethodsList.ToJson());
                }
                Debug.Assert(ParsersMap != null);
                if (ParsersMap.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-parsersmap", ParsersMap.ToJson());
                }
                Debug.Assert(ErrFieldsMap != null);
                if (ErrFieldsMap.Any())
                {
                    MergeAttribute(context.Attributes, $"data-val-{uniqueValidationType}-errfieldsmap", ErrFieldsMap.ToJson());
                }
            }
            catch (Exception e)
            {
                throw new ValidationException(
                          $"{GetType().Name}: collecting of client validation rules for {FieldName} field failed.", e);
            }
        }