Jurioli / Blazor.WebForm.Components

ASP.NET Web Forms System.Web.UI.WebControls Razor Components For Blazor WebAssembly, Blazor Hybrid, Blazor Server.
MIT License
44 stars 9 forks source link

Custom Validator not showing #14

Closed MG1376 closed 5 months ago

MG1376 commented 5 months ago

This is the result of a CustomValidator when it is invalid:

<span data-val-controltovalidate="txtb" data-val-errormessage="Should be even." data-val-isvalid="False" data-val-validationgroup="g1" id="ea8e6d25f8ef438db3376cd76bf6ddd3" data-val="true" data-val-evaluationfunction="CustomValidatorEvaluateIsValid" style="color:Red;font-family:verdana;font-size:10pt;visibility:hidden;">Should be even.</span>

its visibility is hidden. is it a bug?

<asp.TextBox ID="txtb" Text="hello" TextMode="UI.TextBoxMode.Number"
             ValidationGroup="g1" @ref="txtNum">

</asp.TextBox>
<asp.CustomValidator ControlToValidate="txtb" ErrorMessage="Should be even." Display="UI.ValidatorDisplay.Static" ValidationGroup="g1"
            ForeColor="red"
            FontNames="verdana"
            FontSize="10pt"                    
            OnServerValidate="((s, e) => {
                                if (Convert.ToInt32(e.Value) % 2 == 0)
                                    {
                                        e.IsValid = true;
                                    }

                                else
                                    {
                                        e.IsValid = false;
                                    }                                                                //lblLengthError.Visible = !e.IsValid;
                                StateHasChanged();
                            })">
</asp.CustomValidator>
  <asp.Button CausesValidation="true" ValidationGroup="g1" CssClass="btn btn-primary"
              Text="Click" OnClick="((s, e) => { a = Convert.ToInt32(txtNum.Text); StateHasChanged(); })">

  </asp.Button>
MG1376 commented 5 months ago

I solved my problem using this:

  <asp.CustomValidator ControlToValidate="txtb" ErrorMessage="Should be even." Display="UI.ValidatorDisplay.Static" ValidationGroup="g1" @ref="cval"
              OnServerValidate="@((s, e) => {
                                   if (Convert.ToInt32(e.Value) % 2 == 0)
                                      e.IsValid = true;                                              
                                   else 
                                      e.IsValid = false;                             
                                   lbltxtb.Visible = !e.IsValid;     
                                   StateHasChanged();
                                  })">
  </asp.CustomValidator>
  <asp.Label @ref="lbltxtb" CssClass="text-danger">Should be even.</asp.Label>
Jurioli commented 5 months ago

Thank you for your report! I fixed this issue on 2.4.0.6. There are slight changes to WebUIValidation.js.