masastack / MASA.Blazor

Blazor UI component library based on Material Design. Support Blazor Server, Blazor WebAssembly and MAUI Blazor.
https://docs.masastack.com/blazor/getting-started/installation
Other
1.18k stars 159 forks source link

[Bug Report]: BInput Not Vaildating #1497

Closed wisamidris7 closed 1 year ago

wisamidris7 commented 1 year ago

Masa.Blazor version

1.0.2

Describe the bug

I Has DataAnnitaitonsVaildation It Works But SomeTimes I Want To Make Custom Things So I Maked This Class That You Call Him Inside Form And Make Ref From Him And Use It

CustomVaildator.cs

public class CustomValidation : ComponentBase
{
    private ValidationMessageStore? _messageStore;

    [CascadingParameter]
    private EditContext? CurrentEditContext { get; set; }
    [CascadingParameter]
    private BForm Form { get; set; }
    [Inject]
    private MasaLocalization GL { get; set; }
    protected override void OnInitialized()
    {
        if (CurrentEditContext is null && Form is null)
            throw new InvalidOperationException(
                $"{nameof(CustomValidation)} requires a cascading " +
                $"parameter of type {nameof(EditContext)}. " +
                $"For example, you can use {nameof(CustomValidation)} " +
                $"inside an {nameof(EditForm)}.");

        if (CurrentEditContext is null)
            CurrentEditContext = Form.EditContext;

        _messageStore = new(CurrentEditContext);

        CurrentEditContext.OnValidationRequested += (s, e) =>
            _messageStore?.Clear();
        CurrentEditContext.OnFieldChanged += (s, e) =>
            _messageStore?.Clear(e.FieldIdentifier);
    }
    public void DisplayErrors(List<string> errors)
    {
        if (CurrentEditContext is not null && errors is not null)
        {
            foreach (var err in errors)
            {
                var strings = err.Split(":");

                var propName = strings[0];
                var message = strings[1];

                _messageStore?.Add(CurrentEditContext.Field(propName), GL[message]);
            }

            CurrentEditContext.NotifyValidationStateChanged();
        }
    }

    public void ClearErrors()
    {
        _messageStore?.Clear();
        CurrentEditContext?.NotifyValidationStateChanged();
    }
}
<MForm EnableVaildation OnVaildSubmit="OnSubmit">
    <CustomValidation @ref="_cusVaild"/>
    <MTextField @bind-Value="Name"/>
</MForm>

@code{
    public CustomVailation _cusVaild;

    public OnSubmit()
   {
      _cusVaild.DisplayErrors(new(){
        "Name:SomeCustomMessage"
      });
   }
}

But Problem It's Just Showing Error If I Go And Focused Input

If I Don't Focues Input Won't Work

Expected Behavior

No response

Steps To Reproduce

No response

Reproduction code

No response

.NET version

No response

What browsers are you seeing the problem on?

No response

Additional context

No response

capdiem commented 1 year ago

@wisamidris7 https://docs.masastack.com/blazor/components/forms#parse-validationresult

You can use the FormContext.TryParseFromValidation method render the external validation messages.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 4 days with no activity.

github-actions[bot] commented 1 year ago

This issue is closed because it has been open for 3 days with stale.