private async Task HandleUpdate()
        {
            _editContext.MarkAsUnmodified();
            await UpdateModel();

            NavigationManager.NavigateTo("/");
        }
Esempio n. 2
0
        /// <summary>
        /// Clears all validation messages from the <see cref="EditContext"/> of the given <see cref="EditForm"/>.
        /// </summary>
        /// <param name="editForm">The <see cref="EditForm"/> to use.</param>
        /// <param name="revalidate">
        /// Specifies whether the <see cref="EditContext"/> of the given <see cref="EditForm"/> should revalidate after all validation messages have been cleared.
        /// </param>
        /// <param name="markAsUnmodified">
        /// Specifies whether the <see cref="EditContext"/> of the given <see cref="EditForm"/> should be marked as unmodified.
        /// This will affect the assignment of css classes to a form's input controls in Blazor.
        /// </param>
        /// <remarks>
        /// This extension method should be on EditContext, but EditForm is being used until the fix for issue
        /// <see href="https://github.com/dotnet/aspnetcore/issues/12238"/> is officially released.
        /// </remarks>
        public static void ClearValidationMessages(this EditContext editContext, bool revalidate = false, bool markAsUnmodified = false)
        {
            var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            object GetInstanceField(Type type, object instance, string fieldName)
            {
                var fieldInfo = type.GetField(fieldName, bindingFlags);

                return(fieldInfo.GetValue(instance));
            }

            //var editContext = editForm.EditContext == null
            //    ? GetInstanceField(typeof(EditForm), editForm, "_fixedEditContext") as EditContext
            //    : editForm.EditContext;

            var fieldStates     = GetInstanceField(typeof(EditContext), editContext, "_fieldStates");
            var clearMethodInfo = typeof(HashSet <ValidationMessageStore>).GetMethod("Clear", bindingFlags);

            foreach (DictionaryEntry kv in (IDictionary)fieldStates)
            {
                var messageStores = GetInstanceField(kv.Value.GetType(), kv.Value, "_validationMessageStores");
                clearMethodInfo.Invoke(messageStores, null);
            }

            if (markAsUnmodified)
            {
                editContext.MarkAsUnmodified();
            }

            if (revalidate)
            {
                editContext.Validate();
            }
        }
Esempio n. 3
0
        protected async Task HandleValidSubmit()
        {
            if (CanSubmit)
            {
                await OnValidSubmit.InvokeAsync(EditContext);

                EditContext?.MarkAsUnmodified();
            }
        }
    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));
    }
    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));
    }
Esempio n. 6
0
        protected async Task HandleValidSubmit()
        {
            if (!EditContext.Validate())
            {
                return;
            }

            var changes = HandleModificationState.Changes;

            if (!changes.Any())
            {
                await Notifier.NotifyAsync(new Models.Notification
                {
                    Header  = GetModelId(Model),
                    IsError = false,
                    Message = "No changes"
                }).ConfigureAwait(false);

                return;
            }

            Model = Model.Clone();

            Id    = GetModelId(Model);
            IsNew = false;

            var keys = changes.Keys
                       .OrderBy(k => k, this);

            try
            {
                foreach (var key in keys)
                {
                    await HandleMoficationList(key, changes[key])
                    .ConfigureAwait(false);
                }

                await Notifier.NotifyAsync(new Models.Notification
                {
                    Header  = GetModelId(Model),
                    Message = "Saved"
                }).ConfigureAwait(false);
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    await HandleModificationErrorAsync(e).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                await HandleModificationErrorAsync(e).ConfigureAwait(false);
            }
            finally
            {
                changes.Clear();
            }

            EditContext.MarkAsUnmodified();
            await InvokeAsync(StateHasChanged).ConfigureAwait(false);
        }