fluentui-blazor icon indicating copy to clipboard operation
fluentui-blazor copied to clipboard

FluentButton submit does not work outside the EditForm

Open ashimaz opened this issue 2 years ago • 17 comments

🐛 Bug Report

In Blazor 8 EditForm, FluentButton submit does not work outside the EditForm, it works fine with normal button.

💻 Repro or Code Sample

@page "/testcomp"
@rendermode InteractiveAuto
<EditForm Model="@_testModel" OnValidSubmit="HandleValidSubmit" id="testform"> 
    <FluentValidationValidator />
    <ValidationSummary />
    <FluentTextField @bind-Value="_testModel.Name" AriaLabel="Name"></FluentTextField>
</EditForm>
<button type="submit" form="testform">Save</button>
<FluentButton Appearance="Appearance.Accent" IconStart="@(new Icons.Regular.Size16.Save())" Type="Microsoft.FluentUI.AspNetCore.Components.ButtonType.Submit" FormId="testform">Save</FluentButton>

@code {
    private TestModel _testModel = new();

    private void HandleValidSubmit()
    {
        Console.WriteLine("Valid");
    }

    public class TestModel
    {
        public string? Name { get; set; }
    }

    public class TestModelValidator : AbstractValidator<TestModel>
    {
        public TestModelValidator()
        {
            RuleFor(p => p.Name).NotEmpty().WithMessage("Name required");
        }
    }
}

🤔 Expected Behavior

Submit should be working fine after setting the FormId.

🌍 Your Environment

  • OS & Device: Windows 11
  • Browser Microsoft Edge
  • .NET 8, FluentUI 4.2.0

ashimaz avatar Dec 25 '23 09:12 ashimaz

Can't see the repo or code sample...

vnbaaij avatar Dec 25 '23 09:12 vnbaaij

Sorry, copy issue, now updated.

ashimaz avatar Dec 25 '23 09:12 ashimaz

Try it with setting a FormName instead of id

vnbaaij avatar Dec 25 '23 09:12 vnbaaij

<EditForm Model="@_testModel" OnValidSubmit="HandleValidSubmit" FormName="testform"> 

Did not work.

ashimaz avatar Dec 25 '23 09:12 ashimaz

And with FluentButton inside from it is OK, I presume?

Why do you want to have it outside form?

vnbaaij avatar Dec 25 '23 09:12 vnbaaij

Yes, inside the EditForm it works fine.

I am using MudBlazor and Syncfusion for my .Net 7 Project and now I am upgrading to .Net 8 with FluentUI and Syncfusion and removing MudBlazor.

I have lots of forms that are using Syncfution SfDialog, Syncfusion SfDialog Content and Footer is seperate, So I kept form inside Content and Buttons inside Footer.

https://blazor.syncfusion.com/documentation/dialog/template

<SfDialog>
    <DialogTemplates>
        <Header>
            Edit Form
        </Header>
        <Content>
            <EditForm Model="@_testModel" OnValidSubmit="HandleValidSubmit" FormName="testform"> 
                <FluentValidationValidator />
                <ValidationSummary />
                <FluentTextField @bind-Value="_testModel.Name" AriaLabel="Name"></FluentTextField>
            </EditForm>
        </Content>
        <FooterTemplate>
            <FluentButton Appearance="Appearance.Accent" IconStart="@(new Icons.Regular.Size16.Save())" Type="Microsoft.FluentUI.AspNetCore.Components.ButtonType.Submit" FormId="testform">Save</FluentButton>
        </FooterTemplate>
    </DialogTemplates>
</SfDialog>

So was hoping similar behavior from FluentButton as I am changing to FluentUI components.

ashimaz avatar Dec 25 '23 10:12 ashimaz

Please note that it works with normal button and Syncfusion > SfButton

<EditForm Model="@_testModel" OnValidSubmit="HandleValidSubmit" id="testform"> 
    <FluentValidationValidator />
    <ValidationSummary />
    <FluentTextField @bind-Value="_testModel.Name" AriaLabel="Name"></FluentTextField>
</EditForm>
<FluentButton Appearance="Appearance.Accent" IconStart="@(new Icons.Regular.Size16.Save())" Type="Microsoft.FluentUI.AspNetCore.Components.ButtonType.Submit" form="testform">Save</FluentButton>
<SfButton IsPrimary="true" type="submit" form="testform">Primary</SfButton>
<button type="submit" form="testform">Save</button>

ashimaz avatar Dec 26 '23 06:12 ashimaz

Ok, found this is a know issue in the underlying scripts (https://github.com/microsoft/fluentui/issues/31506). I implemented a workaround for it which will be in the next release.

For this to start working though, requirement is that you need to supply both Id and FormId parameters to the button:

@page "/IssueTester"

<EditForm Model="@_testModel" OnValidSubmit="HandleValidSubmit" id="testform"> 
    
    <ValidationSummary />
    <FluentTextField @bind-Value="_testModel.Name" AriaLabel="Name" form="testform"></FluentTextField>
</EditForm>
<FluentButton Id="submit" FormId="testform" Appearance="Appearance.Accent" IconStart="@(new Icons.Regular.Size16.Save())" Type="ButtonType.Submit">Save</FluentButton>

<button type="submit" form="testform">Save</button>

@code {
    private TestModel _testModel = new();

    private void HandleValidSubmit()
    {
        Console.WriteLine("Valid");
    }

    public class TestModel
    {
        public string? Name { get; set; }
    }

vnbaaij avatar Dec 27 '23 16:12 vnbaaij

Hi, was this fixed in V4.3.1?, its still not working.

ashimaz avatar Jan 21 '24 10:01 ashimaz

Yes, the fix is in 4.3.1. Did you see and have you done this:

For this to start working though, requirement is that you need to supply both Id and FormId parameters to the button:

(adn the EditForm needs to have an Id set then of course)

vnbaaij avatar Jan 21 '24 11:01 vnbaaij

oh, thank you. Somehow, I missed reading that part, its working, and i can move forward with FluentUI migration ☺☺☺

ashimaz avatar Jan 22 '24 04:01 ashimaz

Shouldn't this be added to the documentation for the FormId property?

Rocky-25 avatar Feb 13 '25 11:02 Rocky-25

Here's an example that doesn't work. (net8, VS2026.)

@page "/Form"
@rendermode InteractiveServer

@code {
    EditForm TheForm { get; set; }

    class FormModel
    {
        public string StringValue { get; set; }
    }

    FormModel FormValue { get; set; }

    protected override void OnParametersSet()
    {
        FormValue = new FormModel() { StringValue = "string value" };
        base.OnParametersSet();
    }

    void OnValidSubmit()
    {
        
    }
}

<Card Class="mb-3">
    <CardBody>
        <EditForm @ref="@TheForm" FormName="the_form" id="the_form" Model="@FormValue" OnValidSubmit="OnValidSubmit">
            <InputText ValueExpression="@(() => FormValue.StringValue)" />
        </EditForm>
    </CardBody>
</Card>

<button type="submit" form="the_form" class="btn btn-primary">submit from outside form</button>

rwb196884 avatar Dec 04 '25 17:12 rwb196884

@rwb196884 what exactly does your code have to do with the library? I don't see any Fluent components in your code...

vnbaaij avatar Dec 04 '25 17:12 vnbaaij

@vnbaaij it's the same problem; so apparently this is not something to do with Fluent components.

rwb196884 avatar Jan 20 '26 10:01 rwb196884

Yes its working if we put Id="create" to the Form then FormId="create" and Id="whatever" to the button, but why we need Id on button?

<FluentStack Orientation="Orientation.Vertical">
    <FluentEditForm Model="_data"
                    OnValidSubmit="Submit"
                    id="create">
        <DataAnnotationsValidator />
        <ValidationSummary />

        <FluentGrid>
            <FluentGridItem xs="12" sm="12" md="6">
                <FluentTextField Label="Name"
                                 @bind-Value="data.Name"
                                 Required="true" />
                <FluentValidationMessage For="() => data.Name" />
            </FluentGridItem>
            <FluentGridItem xs="12" sm="12" md="12">
                <FluentTextArea Label="Remarks"
                                @bind-Value="data.Remarks"/>
            </FluentGridItem>
        </FluentGrid>
    </FluentEditForm>

    <FluentStack Orientation="Orientation.Horizontal"
                 HorizontalAlignment="HorizontalAlignment.Left"
                 Class="pa-2 mt-4">
        <FluentButton Appearance="Appearance.Accent"
                      Type="ButtonType.Submit"
                      FormId="create"
                      Id="whatever">
            Submit
        </FluentButton>
    </FluentStack>
</FluentStack>

anggaputra-ikt avatar Jan 25 '26 11:01 anggaputra-ikt

@anggaputra-ikt thevid is needed to circumvent a limitation of the underlying web component.

vnbaaij avatar Jan 25 '26 12:01 vnbaaij