public void MemberNameProperty() {
            // Arrange
            ModelValidationResult result = new ModelValidationResult();

            // Act & assert
            MemberHelper.TestStringProperty(result, "MemberName", String.Empty, false);
        }
Ejemplo n.º 2
0
 private static ModelValidationResult CreateSubPropertyResult(ModelMetadata propertyMetadata, ModelValidationResult propertyResult)
 {
     return new ModelValidationResult
     {
         MemberName = DefaultModelBinder.CreateSubPropertyName(propertyMetadata.PropertyName, propertyResult.MemberName),
         Message = propertyResult.Message
     };
 }
Ejemplo n.º 3
0
 private static ModelValidationResult CreateSubPropertyResult(
     ModelMetadata propertyMetadata,
     ModelValidationResult propertyResult
     )
 {
     return(new ModelValidationResult
     {
         MemberName = DefaultModelBinder.CreateSubPropertyName(
             propertyMetadata.PropertyName,
             propertyResult.MemberName
             ),
         Message = propertyResult.Message
     });
 }
Ejemplo n.º 4
0
        public override IEnumerable <ModelValidationResult> Validate(object?container)
        {
            // Per the WCF RIA Services team, instance can never be null (if you have
            // no parent, you pass yourself for the "instance" parameter).

            string memberName = this.Metadata.PropertyName ?? this.Metadata.ModelType.Name;

            var context = new ValidationContext(container ?? this.Metadata.Model)
            {
                DisplayName = this.Metadata.GetDisplayName(),
                MemberName  = memberName
            };

            ValidationResult result = this.Attribute.GetValidationResult(this.Metadata.Model, context);

            if (result != ValidationResult.Success)
            {
                // ModelValidationResult.MemberName is used by invoking validators (such as ModelValidator) to
                // construct the ModelKey for ModelStateDictionary. When validating at type level we want to append the
                // returned MemberNames if specified (e.g. person.Address.FirstName). For property validation, the
                // ModelKey can be constructed using the ModelMetadata and we should ignore MemberName (we don't want
                // (person.Name.Name). However the invoking validator does not have a way to distinguish between these two
                // cases. Consequently we'll only set MemberName if this validation returns a MemberName that is different
                // from the property being validated.

                string?errorMemberName = result.MemberNames.FirstOrDefault();

                if (String.Equals(errorMemberName, memberName, StringComparison.Ordinal))
                {
                    errorMemberName = null;
                }

                var validationResult = new ModelValidationResult {
                    Message    = result.ErrorMessage,
                    MemberName = errorMemberName
                };

                return(new ModelValidationResult[] { validationResult });
            }

            return(Enumerable.Empty <ModelValidationResult>());
        }
        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            // Per the WCF RIA Services team, instance can never be null (if you have
            // no parent, you pass yourself for the "instance" parameter).
            string memberName = Metadata.PropertyName ?? Metadata.ModelType.Name;
            ValidationContext context = new ValidationContext(container ?? Metadata.Model)
            {
                DisplayName = Metadata.GetDisplayName(),
                MemberName = memberName
            };

            ValidationResult result = Attribute.GetValidationResult(Metadata.Model, context);
            if (result != ValidationResult.Success)
            {
                // ModelValidationResult.MemberName is used by invoking validators (such as ModelValidator) to 
                // construct the ModelKey for ModelStateDictionary. When validating at type level we want to append the 
                // returned MemberNames if specified (e.g. person.Address.FirstName). For property validation, the 
                // ModelKey can be constructed using the ModelMetadata and we should ignore MemberName (we don't want 
                // (person.Name.Name). However the invoking validator does not have a way to distinguish between these two 
                // cases. Consequently we'll only set MemberName if this validation returns a MemberName that is different
                // from the property being validated.

                string errorMemberName = result.MemberNames.FirstOrDefault();
                if (String.Equals(errorMemberName, memberName, StringComparison.Ordinal))
                {
                    errorMemberName = null;
                }

                var validationResult = new ModelValidationResult
                {
                    Message = result.ErrorMessage,
                    MemberName = errorMemberName
                };

                return new ModelValidationResult[] { validationResult };
            }

            return Enumerable.Empty<ModelValidationResult>();
        }
 private static void AddModelError(this Controller controller, string prefix, ModelValidationResult validator)
 {
     controller.ModelState.AddModelError(
         CreateSubPropertyName(prefix, validator.MemberName),
         validator.Message);
 }
        /// <summary>
        /// When implemented in a derived class, validates the object.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>
        /// A list of validation results.
        /// </returns>
        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            var validator = (IValidatableObject)_metadata.Model;
            var validationResults = validator.Validate(new ValidationContext(_metadata.Model, null, null));
            foreach (var validationResult in validationResults)
            {
                bool gotMemberNames = false;
                foreach (var memberName in validationResult.MemberNames)
                {
                    gotMemberNames = true;
                    var item = new ModelValidationResult
                    {
                        MemberName = memberName,
                        Message = validationResult.ErrorMessage
                    };
                    yield return item;
                }

                if (!gotMemberNames)
                    yield return new ModelValidationResult
                    {
                        MemberName = string.Empty,
                        Message = validationResult.ErrorMessage
                    };
            }
        }
        /// <summary>
        /// Gets the model validation results.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns>The model validation results.</returns>
        private static IEnumerable<ModelValidationResult> GetModelValidationResults(ValidationResult result)
        {
            ValidationErrorCollection errors = result.Errors;
            ModelValidationResult[] modelValidationResults = new ModelValidationResult[errors.Count];
            for (int i = 0; i < errors.Count; i++)
            {
                ValidationError validationError = errors[i];

                modelValidationResults[i] = new ModelValidationResult
                                                {
                                                    MemberName = validationError.PropertyName,
                                                    Message = validationError.Message
                                                };
            }

            return modelValidationResults;
        }
        private static ModelValidationResult LocalizeValidationResult(ModelValidationResult result, object[] arguments)
        {
            result.Message = ResHelper.GetStringFormat(result.Message, arguments);

            return result;
        }
Ejemplo n.º 10
0
 static ModelValidationResult CreateSubPropertyResult(ModelMetadata propertyMetadata, ModelValidationResult propertyResult) =>
 new ModelValidationResult
 {
     MemberName = CreateSubPropertyName(propertyMetadata.PropertyName, propertyResult.MemberName),
     Message    = propertyResult.Message
 };
Ejemplo n.º 11
0
		public override IEnumerable<ModelValidationResult> Validate(object container)
		{
			var containerValue = _targetProperty.GetValue(container, null);
			var currentValue = containerValue == null ? String.Empty : containerValue.ToString();
			var values = Values == null ? Enumerable.Empty<string>() : Values.Split(new[] { _valueSeparator }, StringSplitOptions.RemoveEmptyEntries);
			if (values == null || !values.Any())
			{
				var valuesList = new List<string>();
				foreach (var value in Enum.GetValues(_targetProperty.PropertyType))
					valuesList.Add(value.ToString());

				values = valuesList.ToArray();
			}

			foreach (var value in values)
			{
				if (value == currentValue)
				{
					return Enumerable.Empty<ModelValidationResult>();
				}
			}

			var result = new ModelValidationResult();
			result.MemberName = _targetProperty.Name;
			result.Message = ErrorMessage;
			return new[] { result };
		}