Ejemplo n.º 1
0
        private void ValidationStateChangeHandler(object?sender, ValidationStateChangedEventArgs eventArgs)
        {
            var state = ValidationState.None;

            if (!EditContext.WasValidated(Property))
            {
                state |= ValidationState.NotValidated;
            }
            else
            {
                if (EditContext.IsValid(Property))
                {
                    state |= ValidationState.Valid;
                }
                else
                {
                    state |= ValidationState.Invalid;
                }
            }

            if (EditContext.IsModified())
            {
                state |= ValidationState.Modified;
            }

            State = state;

            StateHasChanged();
        }
    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));
    }
    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));
    }
    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 async Task Submit(EditContext context)
        {
            Console.WriteLine($"Form is valid: {context.Validate()}");
            Console.WriteLine($"Form is modified: {context.IsModified()}");
            Console.WriteLine($"Form is ValidationMessages: {String.Join(",", context.GetValidationMessages())}");
            if (!context.GetValidationMessages().Any())
            {
                await BookingService.AddBookingAsync(Model);

                NavigationManager.NavigateTo(NavigationManager.BaseUri);
            }
        }
        protected void OnSelectedFieldChanged <T>(string fieldName, FieldChangedEventArgs args, Action <T, FieldIdentifier> action)
            where T : class
        {
            if (args.FieldIdentifier.FieldName.Equals(fieldName) && EditContext.IsModified(args.FieldIdentifier))
            {
                T?model = EditContext.Model as T;

                if (model is not null)
                {
                    action.Invoke(model, args.FieldIdentifier);
                }
            }
        }
    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))));
    }
Ejemplo n.º 8
0
        private void ValidateModel(EditContext editContext, ValidationMessageStore messages, bool onSubmit = false)
        {
            var context          = new ValidationContext <object>(editContext.Model);
            var validationResult = validator.Validate(context);

            messages.Clear();
            foreach (var error in validationResult.Errors)
            {
                var fieldIdentifier = ToFieldIdentifier(editContext, error.PropertyName);
                if (!editContext.IsModified(fieldIdentifier) && !onSubmit)
                {
                    continue;
                }
                messages.Add(fieldIdentifier, error.ErrorMessage);
            }
            editContext.NotifyValidationStateChanged();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get the validation class for the input label based on the input field validation state.
        /// </summary>
        /// <param name="editContext">The <see cref="EditContext"/> of the input field.</param>
        /// <param name="fieldName">The input field name.</param>
        /// <returns>Returns "text-success" or "text-danger" based on the input field's validation state.</returns>
        public static string GetValidationCssClassForLabel(this EditContext editContext, string fieldName)
        {
            if (editContext == null)
            {
                throw new ArgumentNullException(nameof(editContext));
            }

            FieldIdentifier fieldIdentifier = new FieldIdentifier(editContext.Model, fieldName);

            bool isValid = !editContext.GetValidationMessages(fieldIdentifier).Any();

            if (editContext.IsModified(fieldIdentifier))
            {
                return(isValid ? "text-success" : "text-danger");
            }

            return(isValid ? string.Empty : "text-danger");
        }
Ejemplo n.º 10
0
 private void SetIsDisabled()
 {
     _disabled = !EditContext.IsModified();
     StateHasChanged();
 }
    public void IsInitiallyUnmodified()
    {
        var editContext = new EditContext(new object());

        Assert.False(editContext.IsModified());
    }