public void GetsValidationMessagesFromDataAnnotations()
        {
            // Arrange
            var model = new TestModel {
                IntFrom1To100 = 101
            };
            var editContext = new EditContext(model).AddDataAnnotationsValidation();

            // Act
            var isValid = editContext.Validate();

            // Assert
            Assert.False(isValid);

            Assert.Equal(new string[]
            {
                "RequiredString:required",
                "IntFrom1To100:range"
            },
                         editContext.GetValidationMessages());

            Assert.Equal(new string[] { "RequiredString:required" },
                         editContext.GetValidationMessages(editContext.Field(nameof(TestModel.RequiredString))));

            // This shows we're including non-[Required] properties in the validation results, i.e,
            // that we're correctly passing "validateAllProperties: true" to DataAnnotations
            Assert.Equal(new string[] { "IntFrom1To100:range" },
                         editContext.GetValidationMessages(editContext.Field(nameof(TestModel.IntFrom1To100))));
        }
Example #2
0
        public void LookingUpModel_ThatOverridesGetHashCodeAndEquals_Works()
        {
            // Test for https://github.com/aspnet/AspNetCore/issues/18069
            // Arrange
            var model       = new EquatableModel();
            var editContext = new EditContext(model);

            editContext.NotifyFieldChanged(editContext.Field(nameof(EquatableModel.Property)));

            model.Property = "new value";

            Assert.True(editContext.IsModified(editContext.Field(nameof(EquatableModel.Property))));
        }
        public void IgnoresFieldChangesThatDoNotCorrespondToAValidatableProperty(string fieldName)
        {
            // Arrange
            var editContext = new EditContext(new TestModel()).AddDataAnnotationsValidation();
            var onValidationStateChangedCount = 0;

            editContext.OnValidationStateChanged += (sender, eventArgs) => onValidationStateChangedCount++;

            // Act/Assert: Ignores field changes that don't correspond to a validatable property
            editContext.NotifyFieldChanged(editContext.Field(fieldName));
            Assert.Equal(0, onValidationStateChangedCount);

            // Act/Assert: For sanity, observe that we would have validated if it was a validatable property
            editContext.NotifyFieldChanged(editContext.Field(nameof(TestModel.RequiredString)));
            Assert.Equal(1, onValidationStateChangedCount);
        }
            private void OnValidationRequested(object?sender, ValidationRequestedEventArgs e)
            {
                var validationContext = new ValidationContext(_editContext.Model);
                var validationResults = new List <ValidationResult>();

                Validator.TryValidateObject(_editContext.Model, validationContext, validationResults, true);

                // Transfer results to the ValidationMessageStore
                _messages.Clear();
                foreach (var validationResult in validationResults)
                {
                    if (validationResult == null)
                    {
                        continue;
                    }

                    var hasMemberNames = false;
                    foreach (var memberName in validationResult.MemberNames)
                    {
                        hasMemberNames = true;
                        _messages.Add(_editContext.Field(memberName), validationResult.ErrorMessage !);
                    }

                    if (!hasMemberNames)
                    {
                        _messages.Add(new FieldIdentifier(_editContext.Model, fieldName: string.Empty), validationResult.ErrorMessage !);
                    }
                }

                _editContext.NotifyValidationStateChanged();
            }
Example #5
0
        public void CanClearAllModifications()
        {
            // Arrange
            var editContext = new EditContext(new object());
            var field1      = editContext.Field("field1");
            var field2      = editContext.Field("field2");

            editContext.NotifyFieldChanged(field1);
            editContext.NotifyFieldChanged(field2);

            // Act
            editContext.MarkAsUnmodified();

            // Assert
            Assert.False(editContext.IsModified());
            Assert.False(editContext.IsModified(field1));
            Assert.False(editContext.IsModified(field2));
        }
Example #6
0
        public void TracksFieldsAsModifiedWhenValueChanged()
        {
            // Arrange
            var editContext       = new EditContext(new object());
            var fieldOnThisModel1 = editContext.Field("field1");
            var fieldOnThisModel2 = editContext.Field("field2");
            var fieldOnOtherModel = new FieldIdentifier(new object(), "field on other model");

            // Act
            editContext.NotifyFieldChanged(fieldOnThisModel1);
            editContext.NotifyFieldChanged(fieldOnOtherModel);

            // Assert
            Assert.True(editContext.IsModified());
            Assert.True(editContext.IsModified(fieldOnThisModel1));
            Assert.False(editContext.IsModified(fieldOnThisModel2));
            Assert.True(editContext.IsModified(fieldOnOtherModel));
        }
Example #7
0
        public void CanConstructFieldIdentifiersForRootModel()
        {
            // Arrange/Act
            var model           = new object();
            var editContext     = new EditContext(model);
            var fieldIdentifier = editContext.Field("testFieldName");

            // Assert
            Assert.Same(model, fieldIdentifier.Model);
            Assert.Equal("testFieldName", fieldIdentifier.FieldName);
        }
Example #8
0
        public void CanClearIndividualModifications()
        {
            // Arrange
            var editContext               = new EditContext(new object());
            var fieldThatWasModified      = editContext.Field("field1");
            var fieldThatRemainsModified  = editContext.Field("field2");
            var fieldThatWasNeverModified = editContext.Field("field that was never modified");

            editContext.NotifyFieldChanged(fieldThatWasModified);
            editContext.NotifyFieldChanged(fieldThatRemainsModified);

            // Act
            editContext.MarkAsUnmodified(fieldThatWasModified);
            editContext.MarkAsUnmodified(fieldThatWasNeverModified);

            // Assert
            Assert.True(editContext.IsModified());
            Assert.False(editContext.IsModified(fieldThatWasModified));
            Assert.True(editContext.IsModified(fieldThatRemainsModified));
            Assert.False(editContext.IsModified(fieldThatWasNeverModified));
        }
        private static void ValidateModel(EditContext editContext, ValidationMessageStore messages, IStringLocalizer localizer)
        {
            var validator         = GetValidatorForModel(editContext.Model, editContext.Model, localizer);
            var validationResults = validator.Validate(CreateValidationContext(editContext.Model));

            messages.Clear();
            foreach (var validationResult in validationResults.Errors.Distinct(CompareError.Instance))
            {
                messages.Add(editContext.Field(validationResult.PropertyName), validationResult.ErrorMessage);
            }

            editContext.NotifyValidationStateChanged();
        }