コード例 #1
0
            public override IEnumerable <ModelValidationResult> Validate(object container)
            {
                bool propertiesValid = true;

                ModelMetadata[] properties = Metadata.PropertiesAsArray;

                // Performance sensitive loops
                for (int propertyIndex = 0; propertyIndex < properties.Length; propertyIndex++)
                {
                    ModelMetadata propertyMetadata = properties[propertyIndex];
                    foreach (ModelValidator propertyValidator in propertyMetadata.GetValidators(ControllerContext))
                    {
                        foreach (ModelValidationResult propertyResult in propertyValidator.Validate(Metadata.Model))
                        {
                            propertiesValid = false;
                            yield return(CreateSubPropertyResult(propertyMetadata, propertyResult));
                        }
                    }
                }

                if (propertiesValid)
                {
                    foreach (ModelValidator typeValidator in Metadata.GetValidators(ControllerContext))
                    {
                        foreach (ModelValidationResult typeResult in typeValidator.Validate(container))
                        {
                            yield return(typeResult);
                        }
                    }
                }
            }
コード例 #2
0
        private static FieldValidationMetadata ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string modelName)
        {
            FieldValidationMetadata validationMetadataForField = htmlHelper.ViewContext.FormContext.GetValidationMetadataForField(modelName, true);

            foreach (IEnumerable <ModelClientValidationRule> rules in
                     (from v in modelMetadata.GetValidators(htmlHelper.ViewContext.Controller.ControllerContext) select v.GetClientValidationRules()))
            {
                foreach (var rule in rules)
                {
                    validationMetadataForField.ValidationRules.Add(rule);
                }
            }
            return(validationMetadataForField);
        }