Example #1
0
        public void Validate(ActionContext actionContext, ValidationStateDictionary validationState, string prefix, object model)
        {
            var requiredErrorsNotHandledByFv = RemoveImplicitRequiredErrors(actionContext);

            // Apply any customizations made with the CustomizeValidatorAttribute
            var metadata = model == null ? null : _modelMetadataProvider.GetMetadataForType(model.GetType());

            if (model != null)
            {
                var customizations = GetCustomizations(actionContext, model.GetType(), prefix);
                actionContext.HttpContext.Items["_FV_Customizations"] = Tuple.Create(model, customizations);
            }

            // Setting as to whether we should run only FV or FV + the other validator providers
            var validatorProvider = _runMvcValidation ? _compositeProvider : _fvProvider;

            var visitor = new FluentValidationVisitor(
                actionContext,
                validatorProvider,
                _validatorCache,
                _modelMetadataProvider,
                validationState)
            {
                ValidateChildren = _implicitValidationEnabled
            };

            visitor.Validate(metadata, prefix, model);

            // Re-add errors that we took out if FV didn't add a key.
            ReApplyImplicitRequiredErrorsNotHandledByFV(requiredErrorsNotHandledByFv);

            // Remove duplicates. This can happen if someone has implicit child validation turned on and also adds an explicit child validator.
            RemoveDuplicateModelstateEntries(actionContext);
        }
        public override ValidationVisitor GetValidationVisitor(ActionContext actionContext, IModelValidatorProvider validatorProvider, ValidatorCache validatorCache, IModelMetadataProvider metadataProvider, ValidationStateDictionary validationState)
        {
            // Setting as to whether we should run only FV or FV + the other validator providers
            var validatorProviderToUse = _runMvcValidation ? validatorProvider : _fvProvider;

            var visitor = new FluentValidationVisitor(
                actionContext,
                validatorProviderToUse,
                validatorCache,
                metadataProvider,
                validationState);

            return(visitor);
        }