コード例 #1
0
        public static ValidationResult Create(RuleValidator validator, RuleValidatorContext context, IList<Object> parameterValues, object messageKey, IEnumerable<ValidationResult> nestedValidationResults = null)
        {
            string message = string.Empty;
            var messageService = new MessageService();

            if (String.IsNullOrEmpty(validator.Message))
            {
                var messageContext = new MessageContext(context, validator.GetType(), validator.Negate, validator.MessageStoreName, messageKey, validator.MessageFormatter);
                message = messageService.GetDefaultMessageAndFormat(messageContext, parameterValues);
            }
            else
            {
                //Since the message was supplied, don't get the default message from the store, just format it
                message = messageService.FormatMessage(validator.Message, context, parameterValues, validator.MessageFormatter);
            }

            //Override level if all the nested validation errors are Warnings

            if (nestedValidationResults != null && nestedValidationResults.All(vr => vr.Level == ValidationLevelType.Warn))
            {
                return new ValidationResult(context.PropertyInfo, message, ValidationLevelType.Warn, context.PropertyValue, nestedValidationResults);
            }
            else
            {
                return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults);
            }

            //return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults);
        }
コード例 #2
0
        public ModelClientValidationRule Create(RuleValidator ruleValidator)
        {
            var clientRule = new ModelClientValidationRule();

            var ruleValidatorType = ruleValidator.GetType().GetGenericTypeDefinition();
            if (!Mapping.ContainsKey(ruleValidatorType))
            {
                return null;
            }

            var rule = Mapping[ruleValidatorType];

            clientRule.ValidationType = rule.JQueryRuleName;
            clientRule.ErrorMessage = ruleValidator.ErrorMessageTemplate;

            //map all the parameters
            foreach (var parameter in rule.Parameters)
            {
                // parameter.value is index of the matching expression in the rulevalidator PropertyExpressions collection
                var ruleParamQry = from ruleParameter in ruleValidator.Params
                                   where ruleParameter.PropertyName == parameter.Value
                                   select ruleParameter;

                if (ruleParamQry.Any())
                {
                    var ruleParam = ruleParamQry.First();
                    if (ruleParam.IsExpressionParam)
                    {
                        var expression = ruleParam.CompiledExpression.Expression;
                        if (expression.Body.NodeType == ExpressionType.MemberAccess)
                        {
                            var propertyName = ((MemberExpression)expression.Body).Member.Name;
                            var propertyType = ((MemberExpression) expression.Body).Type;
                            clientRule.ValidationParameters.Add(parameter.Key,
                                                                new PropertyExpressionParam() { PropertyName = propertyName, IsDate = propertyType == typeof(DateTime)});
                        }
                    }
                    else
                    {
                        var paramValue = ruleParam.GetParamValue();
                        if (paramValue is DateTime)
                        {
                            clientRule.ValidationParameters.Add(parameter.Key, new DateTimeParam(ruleParam.GetParamValue()));
                        }
                        else
                        {
                            //parameter.value is the index of the matching value in the rulevalidator parameters collection
                            clientRule.ValidationParameters.Add(parameter.Key, ruleParam.GetParamValue());
                        }
                    }
                }
            }

            return clientRule;
        }
コード例 #3
0
        public static ValidationResult Create(RuleValidator validator, RuleValidatorContext context, IList<Object> parameterValues, object messageKey)
        {
            string message = string.Empty;
            var messageService = new MessageService();

            if (String.IsNullOrEmpty(validator.Message))
            {
                var messageContext = new MessageContext(context, validator.GetType(), validator.Negate, validator.MessageStoreName, messageKey, validator.MessageFormatter);
                message = messageService.GetDefaultMessageAndFormat(messageContext, parameterValues);
            }
            else
            {
                //Since the message was supplied, don't get the default message from the store, just format it
                message = messageService.FormatMessage(validator.Message, context, parameterValues, validator.MessageFormatter);
            }

            return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue);
        }
コード例 #4
0
ファイル: RuleNode.cs プロジェクト: rbell/SpecExpress
 public RuleNode(RuleValidator rule)
 {
     Rule = rule;
 }
コード例 #5
0
 public abstract void AddRule(RuleValidator ruleValidator);