private async Task HandleSubmitAsync() { if (OnSubmit.HasDelegate) { await OnSubmit.InvokeAsync(_fixedEditContext); } else { var isValid = _fixedEditContext.Validate(); if (isValid) { if (OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(_fixedEditContext); } else { await HandleValidSubmit(); } } if (!isValid && OnInvalidSubmit.HasDelegate) { await OnInvalidSubmit.InvokeAsync(_fixedEditContext); } } }
/// <summary> /// 提交指定 <see cref="CascadedEditContext"/> 的表单。 /// </summary> public async Task Submit() { if (CascadedEditContext == null) { throw new InvalidOperationException($"未设置 {nameof(CascadedEditContext)} 的表单,无法进行提交,请使用 {nameof(OnClick)} 触发点击按钮的事件。"); } var valid = CascadedEditContext.Validate(); if (valid) { IsSubmitting = true; await Disable(); await Task.Delay(ValidSubmitDelay); await OnValidSubmit.InvokeAsync(CascadedEditContext); IsSubmitting = false; await Disable(false); } else { await OnInvalidSubmit.InvokeAsync(CascadedEditContext); } }
private async Task HandleSubmitAsync() { var validatorType = typeof(IValidator <>).MakeGenericType(_fixedEditContext.Model.GetType()); var validators = ServiceProvider.GetServices(validatorType).OfType <IValidator>(); var isValid = true; _validationMessageStore.Clear(); foreach (var validatorRuleSet in validators) { var validationResults = await validatorRuleSet.ValidateAsync(_fixedEditContext.Model); foreach (var validationResult in validationResults.Errors) { var fieldIdentifier = ToFieldIdentifier(_fixedEditContext, validationResult.PropertyName); _validationMessageStore.Add(fieldIdentifier, validationResult.ErrorMessage); isValid = false; } } if (isValid && OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(_fixedEditContext); } if (!isValid && OnInvalidSubmit.HasDelegate) { await OnInvalidSubmit.InvokeAsync(_fixedEditContext); } }
private async Task HandleSubmitAsync() { if (OnSubmit is object) { // When using OnSubmit, the developer takes control of the validation lifecycle var submitSuccess = await OnSubmit.Invoke(_fixedEditContext); if (submitSuccess) { await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", Id); } } else { // Otherwise, the system implicitly runs validation on form submission var isValid = _fixedEditContext.Validate(); // This will likely become ValidateAsync later if (isValid && OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(_fixedEditContext); // Clear saved form model from local storage await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", Id); } if (!isValid && OnInvalidSubmit.HasDelegate) { await OnInvalidSubmit.InvokeAsync(_fixedEditContext); } } }
private async Task HandleSubmitAsync() { Debug.Assert(_fixedEditContext != null); if (OnSubmit.HasDelegate) { // When using OnSubmit, the developer takes control of the validation lifecycle await OnSubmit.InvokeAsync(_fixedEditContext); } else { // Otherwise, the system implicitly runs validation on form submission var isValid = _fixedEditContext.Validate(); // This will likely become ValidateAsync later if (isValid && OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(_fixedEditContext); } if (!isValid && OnInvalidSubmit.HasDelegate) { await OnInvalidSubmit.InvokeAsync(_fixedEditContext); } } }
protected async Task HandleValidSubmit() { if (CanSubmit) { await OnValidSubmit.InvokeAsync(EditContext); EditContext?.MarkAsUnmodified(); } }
/// <summary> /// Handler for when the form has been successfully submitted. /// </summary> /// <param name="context">Edit context.</param> public void HandleOnValidSubmit(EditContext context) { if (!OnValidSubmit.HasDelegate) { return; } OnValidSubmit.InvokeAsync(context); StateHasChanged(); }
private async Task OnDataAnnotationsValidated() { Movie.MoviesGenres = Selected .Select(mg => new MoviesGenres { GenreId = int.Parse(mg.Key) }).ToList(); Movie.MoviesActors = SelectedActors .Select(ma => new MoviesActors { PersonId = ma.Id, Character = ma.Character }).ToList(); if (!string.IsNullOrWhiteSpace(Movie.Poster)) { imageUrl = null; } await OnValidSubmit.InvokeAsync(null); }
private async Task <bool> CascadedFormContext_OnFormSubmit(object sender, MFormContainerContextSubmitArgs e) { // Console.WriteLine("FormContextSubmit: " + typeof(T)); var isValid = mEditContext.Validate(); // This will likely become ValidateAsync later if (!isValid) { Console.WriteLine(typeof(T) + ": Not valid"); // if (ContainerContext != null) // throw new UserMessageException(L["Please check all forms. There is at least one validation error!"]); return(false); } Dictionary <string, object> changedDict = new Dictionary <string, object>(); if (HasUnsavedChanges) { foreach (var entry in ChangedValues) { var fullname = entry.GetFullName(); if (changedDict.ContainsKey(fullname)) { continue; } object value = entry.GetValue(Model); changedDict.Add(fullname, value); } ChangedValues.Clear(); } if (OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(new MFormSubmitArgs(mEditContext, changedDict, Model, e.UserInterated)); } return(true); }
private void CascadedFormContext_OnFormSubmit(object sender, MFormContainerContextSubmitArgs e) { // Console.WriteLine("FormContextSubmit: " + typeof(T)); var isValid = mEditContext.Validate(); // This will likely become ValidateAsync later if (!isValid) { if (ContainerContext != null) { throw new UserMessageException("Please check the values. There is at least one validation error!"); } Console.WriteLine(typeof(T) + ": Not valid!"); return; } Dictionary <string, object> changedDict = new Dictionary <string, object>(); if (HasUnsavedChanges) { foreach (var entry in ChangedValues) { string fieldname = entry.FieldName; if (changedDict.ContainsKey(fieldname)) { continue; } //01.08.2020 entry.Model is an old wrong version of the model in client side object value = ReflectionHelper.GetPropertyValue(Model, entry.FieldName); changedDict.Add(fieldname, value); } ChangedValues.Clear(); } if (OnValidSubmit.HasDelegate) { var task = OnValidSubmit.InvokeAsync(new MFormSubmitArgs(mEditContext, changedDict, Model, e.UserInterated)); task.Wait(); } }
#pragma warning disable CA2007 private async Task HandleSubmitAsync() { Debug.Assert(_extensibleEditContext != null); var isValid = _extensibleEditContext !.Validate(); if (isValid) { if (OnValidSubmit.HasDelegate) { await OnValidSubmit.InvokeAsync(_extensibleEditContext.RootEditContext); } await _extensibleEditContext.OnValidSubmit(); } else { if (OnInvalidSubmit.HasDelegate) { await OnInvalidSubmit.InvokeAsync(_extensibleEditContext.RootEditContext); } await _extensibleEditContext.OnInvalidSubmit(); } }