dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.59k stars 10.06k forks source link

Async validation enhancements #7680

Open SteveSandersonMS opened 5 years ago

SteveSandersonMS commented 5 years ago

Follows on from #7614

Design ideas:

 * - Add method: editContext.AddValidationTask(FieldIdentifier f, Task t)
 *   It adds the task to a HashSet<Task> on both the FieldState and the EditContext,
 *   so we can easily get all the tasks for a given field and across the whole EditContext
 *   Also it awaits the task completion, and then regardless of outcome (success/fail/cancel),
 *   it removes the task from those hashsets.
 * - Add method: editContext.WhenAllValidationTasks()
 *   Add method: editContext.WhenAllValidationTasks(FieldIdentifier f)
 *   These return Task.WhenAll(hashSet.Values), or Task.Completed if there are none
 * - Optionally also add editContext.HasPendingValidationTasks()
 * - Add method: editContext.ValidateAsync() that awaits all the validation tasks then
 *   returns true if there are no validation messages, false otherwise
 * - Now a validation library can register tasks whenever it starts an async validation process,
 *   can cancel them if it wants, and can still issue ValidationResultsChanged notifications when
 *   each task completes. So a UI can determine whether to show "pending" state on a per-field
 *   and per-form basis, and will re-render as each field's results arrive.
 * - Note: it's unclear why we'd need WhenAllValidationTasks(FieldIdentifier) (i.e., per-field),
 *   since you wouldn't "await" this to get per-field updates (rather, you'd use ValidationResultsChanged).
 *   Maybe WhenAllValidationTasks can be private, and only called by ValidateAsync. We just expose
 *   public HasPendingValidationTasks (per-field and per-edit-context).

Will implement this shortly after getting more of the system in place, assuming it still appears to be the correct design.

mkArtakMSFT commented 5 years ago

@SteveSandersonMS, how important is this to be done in 3.0? Can we do this post 3.0 without breaking customers?

SteveSandersonMS commented 5 years ago

@mkArtakMSFT Yes. Until we make built-in support, it would be extra work for developers to track the set of pending tasks associated with form validation so they can block submission until they are all done, but it is technically possible to do it.

IvanJosipovic commented 3 years ago

@SteveSandersonMS, this issue is blocking using FluentValidation with Async Validators like http calls etc, which are crucial for complex validations. See this issue, https://github.com/Blazored/FluentValidation/issues/38

Do you have any guidance on how to fix this?

isobel-cullen commented 3 years ago

I would like the ability to perform some async action when the context is changed, at the moment I cannot use OnFieldChanged without locking up my application during the async call.

SteveSandersonMS commented 3 years ago

Since this is not a built-in concept in EditContext today, you'd need to write your own code to co-ordinate it. For example, during a form submission, you could start a set of async validation operations and display a "please wait" UI. When those operations complete, if you consider the validation to have passed you would run whatever save logic you need, or if you consider it to have failed, you would add some validation messages to the ValidationMessageStore.

ghost commented 2 years ago

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

AntMaster7 commented 1 year ago

@SteveSandersonMS How about making an async OnValidationRequested event handler in the EditContext? This way the HandleSubmitAsync method of the EditForm which is already async, could invoke and await an async form validation. This async form validation is in the end only triggered when the form is actually being submitted and not on each field changed event. Its then up to a custom validator component to notify the UI about an ongoing validation progress. I created a code example in issue #51501.

AntMaster7 commented 1 year ago

@SteveSandersonMS Any update on this?

ghost commented 11 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

austins commented 10 months ago

Asynchronous OnValidationRequested support would be good, especially now that we have Blazor static SSR. For apps where all forms are handled in this rendering model without interactivity, it would be beneficial.