chanan / BlazorStrap

Bootstrap 4 Components for Blazor Framework
https://blazorstrap.io
The Unlicense
917 stars 157 forks source link

Can't seem to access EditContext #250

Closed Gander7 closed 4 years ago

Gander7 commented 4 years ago

I have a form with 4 radio buttons that act like a status, and the validation of the form needs to change and trigger, based on the radio button value. I have everything working except for triggering a validation of the entire form when the radio button value changes.

I wanted to use EditContexts Validate or something similar but can't seem to access it. Is there a way to access BSForms EditContext or another way to achieve this?

Gander7 commented 4 years ago

For example, given the following form:

<BSForm Model="@Model" ValidateOnInit="true">
    <BSRow>
        <BSFormGroup>
            <BSLabel For="Field1">Field 1, Required on "Status 2"</BSLabel>
            <BSInput InputType="InputType.Text" Id="Field1" @bind-Value="Model.Field1" Size="Size.Small" ValidateOnChange="true" />
         </BSFormGroup>
    </BSRow>

    <BSRow>
        <BSLabel For="Status">Status</BSLabel>
    </BSRow>
    <BSRow>
        <BSCol MD="6">
            <BSFormGroup IsCheck="true">
                <BSInput InputType="InputType.Radio" RadioValue="Status1" 
                    @bind-Value="@CurrentStatus"
                    @oninput="UpdateValidationFromStatus" Id="Status1/>
                 <BSLabel For="Status1">Status 1</BSLabel>
             </BSFormGroup>
        </BSCol>
        <BSCol MD="6">
            <BSFormGroup IsCheck="true">
                <BSInput InputType="InputType.Radio" RadioValue="Status2" 
                    @bind-Value="@CurrentStatus"
                    @oninput="UpdateValidationFromStatus" Id="Status2/>
                 <BSLabel For="Status2">Status 1</BSLabel>
             </BSFormGroup>
        </BSCol>
    </BSRow>

    <BSButton Color="Color.Primary" ButtonType="ButtonType.Submit">Submit</BSButton>
</BSForm>

@code {
    class CustomClass {
        [RequiredWhenStatus2CustomValidationAttribute]
        public string Field1 { get; set; };
        public string Status { get; set; } = "Status1";
    }

    protected string CurrentStatus { get; set; };
    protected CustomClass Model { get; set; } = new CustomClass();

    protected void UpdateValidationFromStatus() 
    {
        Model.Status = CurrentStatus;
        // How do I trigger the validation for entire form, such as Field1
        // EditContext.ForceValidate()?
    }
}
Gander7 commented 4 years ago

Would it be as easy as exposing EditContext as a public Parameter? Then a dev could do <BSForm BSContext="@MyContext"> and both the BSForm and the dev could use it.

jbomhold3 commented 4 years ago

After reviewing this more I deleted my old response. We are using the EditForm Component from Blazor. This is why it's interchangeable with Default components. Just like the default DataAnnotationsValidator you would nest it inside of your bsform then it has full access to the edit context. EditContext is cascaded down this is something I hope we have better access to in 3.1 .

To use ForceValidate <BSForm @ref="BsFormRef">

@code { private BsForm BsFormRef {get;set}

blah.. { BsFormRef.ForceValidate(); } }

Gander7 commented 4 years ago

Got it working using @ref and BSForm.ForceValidate(). Thanks for the help and direction :) I'm good for this to be closed if you are.