Completed
Last Updated: 11 Apr 2022 11:51 by ADMIN
Release 3.2.0
Michael Yereniuk
Created on: 14 Jul 2021 13:10
Category: Form
Type: Bug Report
7
Validation does not trigger when the model comes from an async method

The Validation does not trigger when I load my model from an async method.

 

<AdminEdit>

A workaround for the time being:

@inject HttpClient http
@using System.ComponentModel.DataAnnotations

<TelerikForm @ref="@EditForm" 
             Model="@TheCatFactTelerik" 
             ValidationMessageType="@FormValidationMessageType.None">
    <FormValidation>
        <ValidationSummary />
    </FormValidation>
    <FormItems>
        <FormItem Field="fact">
        </FormItem>
    </FormItems>
    <FormButtons>
        <TelerikButton ButtonType="ButtonType.Submit" Primary="true">Apply</TelerikButton>
    </FormButtons>
</TelerikForm>

@code {
    public TelerikForm EditForm { get; set; }

    public CatFact TheCatFactTelerik = new CatFact();

    public EditContext editContext { get; set; }

    protected async override Task OnInitializedAsync()
    {
        TheCatFactTelerik = await http.GetFromJsonAsync<CatFact>("https://catfact.ninja/fact");
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        EditForm.EditContext.AddDataAnnotationsValidation();
    }

    public class CatFact
    {
        [Required(ErrorMessage = "Required Error Text")]
        [MinLength(10, ErrorMessage = "Min Length 10")]
        public string fact { get; set; }
        public int length { get; set; }
    }
}

</AdminEdit>

1 comment
Paul Wood
Posted on: 17 Oct 2021 07:58

Surely this is a very common scenario that needs to be resolved sooner rather than later.

Also this workaround creates duplicate error messages for me which can be resolved by going:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender) {
await Task.Run(() => editForm.EditContext.AddDataAnnotationsValidation());
}
}