asp.net core 3.1 with Blazorise not reseting Validations with ValidateAll()

Viewed 667

I am creating a Blazor client app using Blazorise Bootstrap.

In my code I have a popup for basic CRUD on a POCO. I am using the Blazorise Validations to enforce field requirements;

Blazorise Validation Documentation

Now the issue I have is that my popup is reusable for adding and editing the POCOs (from a datagrid). I bind the Validation to a reference as follows;

               <Validations @ref="companyValidations" Mode="ValidationMode.Auto">
                    <Fields>
                        <Field IsHorizontal="true" ColumnSize="ColumnSize.Is5.OnDesktop">
                            <Validation Validator="@ValidationRule.IsNotEmpty">
                                <FieldLabel ColumnSize="ColumnSize.Is3.OnDesktop">Company</FieldLabel>
                                <FieldBody ColumnSize="ColumnSize.Is9.OnDesktop">
                                    <TextEdit @bind-Text="@Company.Name" Placeholder="Enter the Company Name..">
                                        <Feedback>
                                            <ValidationError>Please enter a Name for the Company!</ValidationError>
                                        </Feedback>
                                    </TextEdit>
                                </FieldBody>
                            </Validation>
                        </Field>

                        <Field IsHorizontal="true" ColumnSize="ColumnSize.Is5.OnDesktop">
                            <Validation Validator="@ValidationRule.IsNotEmpty">
                                <FieldLabel ColumnSize="ColumnSize.Is3.OnDesktop">Website</FieldLabel>
                                <FieldBody ColumnSize="ColumnSize.Is9.OnDesktop">
                                    <TextEdit @bind-Text="@Company.Website" Placeholder="Enter the Web Address..">
                                        <Feedback>
                                            <ValidationError>Please enter a Website Address for the Company!</ValidationError>
                                        </Feedback>
                                    </TextEdit>
                                </FieldBody>
                            </Validation>
                        </Field>
                    </Fields>
                </Validations>

with

Validations companyValidations = new Validations();

in my Code section. This works fine the first time, however, if I then reuse the component and reset the Company instance, the previous validation are being shown? For example I would load the popup and enter a valid Company.Name and Company.Email. I would then reload the popup and reset using Company = new Company(); which clears the text box, but for some reason calling companyValidations.ValidateAll() is returning true still?

For completion I do this through a public method on the component;

public void Reset()
{
    Company = new CompanyInputModel();      

    companyValidations.ValidateAll();
    companyValidations.ClearAll();     
    StateHasChanged();
}

I have played around with the order however, I cannot seem to reset the validations to 'refresh' when I reset the bound model??

I have tried setting hte ValidationMode to both Validation.Auto and Validation.Manual but the issue persists?

1 Answers

I believe the Blazorise validation component keeps your validations data into your companyValidations So please reinitialize your companyValidation and check it again.

public void Reset()
{
    Company = new CompanyInputModel();      
    Validations companyValidations = new Validations(); // reinitialize validations

    companyValidations.ValidateAll();
    companyValidations.ClearAll();     
    StateHasChanged();
}
Related