/// <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(MethodsList != null);
                if (MethodsList.Any())
                {
                    rule.ValidationParameters.Add("methodslist", MethodsList.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);
            }
        }
Example #2
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);
            }
        }