chanan / BlazorStrap

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

BSForm and @formName #614

Closed DerekChasse closed 2 months ago

DerekChasse commented 3 months ago

I've got a form that I'm having issues submitting with a relatively basic example...

I'm not seeing anything remarkably different from the various sample forms that I've seen referenced.

I'm using the latest version at time of writing. 5.2.100

The POST request does not specify which form is being submitted. To fix this, ensure <form> elements have a @formname attribute with any unique value, or pass a FormName parameter if using <EditForm>.

SignIn.razor

<BSForm Model="@Input" OnValidSubmit="@LoginUser">
    <DataAnnotationsValidator />
    <BSRow Class="gy-3">
        <BSCol Column="12" Class="col-xl-12">
            <BSLabel Class="text-default op-8">Email address</BSLabel>
            <BSInputGroup>
                <BSInput InputType="InputType.Email" InputSize="Size.Large" placeholder="Email" @bind-Value="Input.Email" />
            </BSInputGroup>
        </BSCol>
        <BSCol Column="12" Class="col-xl-12">
            <BSLabel Class="text-default d-block op-8">
                Password
                <BSLink href="reset" Class="float-end text-success">Forget password ?</BSLink>
            </BSLabel>
            <BSInputGroup>
                <BSInput InputType="InputType.Password" InputSize="Size.Large" placeholder="Password" @bind-Value="Input.Password" />
            </BSInputGroup>
        </BSCol>
        <BSCol Column="12" Class="col-xl-12 d-grid" MarginTop="Margins.Large">
            <BSButton IsSubmit="true" Color="BSColor.Primary" Size="Size.Large">Sign In</BSButton>
        </BSCol>
    </BSRow>
</BSForm>

SignIn.razor.cs

public partial class SignIn
{

    [SupplyParameterFromForm]
    private InputModel Input { get; set; } = new();

    public async Task LoginUser()
    {
       // Logic

    }

    private sealed class InputModel
    {
        [Required]
        [EmailAddress]
        public string Email { get; set; } = "";

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; } = "";
    }
jbomhold3 commented 3 months ago

That wouldn't do a post request. It would simply call the function. Make sure you are not nested in another form

DerekChasse commented 3 months ago

Then I'm confused as to BSForm's functionality then...

image

The form element is specified as a post... and I'm not nested within another form.

jbomhold3 commented 3 months ago

Are you using the new static server rendering? If not I'm confused about why it's doing a post instead of directly calling the function. But regardless should be able to just pass the @formname attribute like it's asking for.

jbomhold3 commented 3 months ago

Tested is indeed something for static and do see the issue fixing will post fix today

jbomhold3 commented 3 months ago

https://www.nuget.org/packages/BlazorStrap/5.2.102-Preview1
Adds support for static forms please bring in the V4 or V5 with the same version number

jbomhold3 commented 3 months ago

Example.

@page "/counter"
@using System.ComponentModel.DataAnnotations

<BSForm Model="Model" OnValidSubmit="Submit" FormName="Starship1">
    <DataAnnotationsValidator />
    <div>
        <label>
            Identifier:
            <BSInput InputType="InputType.Text" @bind-Value="Model!.Id" />
            <BSFeedback For="@(() => Model.Id)" ValidMessage="ID looks good." />

        </label>
    </div>
    <div>
        <BSButton IsSubmit="true" Color="BSColor.Primary">Submit</BSButton>
    </div>
</BSForm>
@code {
    [SupplyParameterFromForm]
    public Starship? Model { get; set; }

    protected override void OnInitialized()
    {
        Model ??= new();
    }

    private void Submit()
    {
        Console.WriteLine($"Model.Id: {Model!.Id}");
    }

    public class Starship
    {
        [Required(ErrorMessage = "ID must be provided.")]
        public string? Id { get; set; }
    }
}
DerekChasse commented 3 months ago

Confirming the fix.

Thanks for the quick turnaround!