Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation breaks the formaction attribute #39

Closed
7702244 opened this issue Apr 13, 2023 · 7 comments · Fixed by #92
Closed

Validation breaks the formaction attribute #39

7702244 opened this issue Apr 13, 2023 · 7 comments · Fixed by #92

Comments

@7702244
Copy link

7702244 commented Apr 13, 2023

Suppose there is the following form:

<form action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <button type="submit">Submit</button><br>
  <button type="submit" formaction="/action_page2.php">Submit to another page</button>
  <button type="submit" name="param" value="val">Submit with param</button>
</form>

When using aspnet-client-validation, the form does not take into account the formaction attribute and the key/value passed to submit (if it was clicked). The only working solution is to add formnovalidate attribute to buttons.

<form action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <button type="submit">Submit</button><br>
  <button type="submit" formnovalidate="formnovalidate" formaction="/action_page2.php">Submit to another page</button>
  <button type="submit" formnovalidate="formnovalidate" name="param" value="val">Submit with param</button>
</form>
@haacked
Copy link
Owner

haacked commented Jul 3, 2023

When using aspnet-client-validation, the form does not take into account the formaction attribute and the key/value passed to submit (if it was clicked).

What is the behavior you observed and what is the behavior you expect?

@Forthoney
Copy link

Just hijacking the thread to report the same issue where the form action is ignored. I also wanted to add more detail to what is happening vs what should happen
The context in which I noticed this is using asp-page-handler on my submit button like so:
<input type="submit" asp-page-handler="example" />
I expect to be routed to something like destinationPage/5?handler=example.
But using the validation routes me to destinationPage/5, as confirmed by loggingin the console.

Here's the exact discrepancy I am encountering.
image
image

Considering asp-page-handler is a built in component to Razor pages and needing multiple POST methods is a fairly common occurance, I think this is a pretty critical bug. I haven't looked too deeply into the codebase, so I can't offer much insight into where the bug may be coming from, but if you have some ideas on where the problem may lie, I can try taking this on

@haacked
Copy link
Owner

haacked commented Dec 12, 2023

I was not able to reproduce this. I added a demo page for formaction here: #84

Here's the razor code:

@page
@model DemoWeb.Pages.Demos.FormAction


@{
    Layout = "Shared/_Layout";
}

<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/>

<form method="post">
    <input type="submit" value="Submit" asp-page-handler="Submit" />
    <input type="submit" value="Save" asp-page-handler="Save"/>
</form>

Here's the code behind:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DemoWeb.Pages.Demos;

public class FormAction : PageModel
{
    [TempData]
    public string? StatusMessage { get; set; }


    public IActionResult OnPostSubmitAsync()
    {
        StatusMessage = "Submit button clicked";

        return RedirectToPage();
    }

    public IActionResult OnPostSave()
    {
        StatusMessage = "Save button clicked";

        return RedirectToPage();
    }
}

And it works fine as the screenshots show.

Screenshot 2023-12-12 at 12 10 25 PM Screenshot 2023-12-12 at 12 10 32 PM

@JLS-Envision
Copy link

@haacked I have been running into this issue and can reproduce it. It seems you have to have some form fields in addition to the buttons on the form to trigger the issue. I have been having some trouble getting the source to build locally and cannot get a proper pull request together right now, but if you make some minor modifications to the FormAction page mentioned above you should be able to reproduce the problem.

FormAction.cshtml.cs becomes:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace DemoWeb.Pages.Demos;

public class FormAction : PageModel
{
    [TempData]
    public string? StatusMessage { get; set; }

    [BindProperty]
    public InputModel Input { get; set; }

    public class InputModel
    {
        [Display(Name = "Name")]
        [Required, StringLength(50)]
        public string? Name { get; set; }
    }

    public IActionResult OnPostSubmitAsync()
    {
        StatusMessage = "Submit button clicked";

        return RedirectToPage();
    }

    public IActionResult OnPostSave()
    {
        StatusMessage = "Save button clicked";

        return RedirectToPage();
    }
}

FormAction.cshtml becomes:

@page
@model DemoWeb.Pages.Demos.FormAction

@{
    Layout = "Shared/_Layout";
}

<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/>

<form method="post">
    <div asp-validation-summary="All">The following problems occurred when submitting the form:</div>
    <div>
        <label asp-for="Input.Name"></label>
        <input asp-for="Input.Name" />
        <span asp-validation-for="Input.Name" ></span>
    </div>
    <input type="submit" value="Submit" asp-page-handler="Submit" />
    <input type="submit" value="Save" asp-page-handler="Save"/>
</form>

Hope this helps.

@haacked
Copy link
Owner

haacked commented Jan 2, 2024

I have been having some trouble getting the source to build locally

What issues? Mind filing another issue for that?

@haacked
Copy link
Owner

haacked commented Jan 2, 2024

Hope this helps.

That helped a lot! I can confirm the issue.

@JLS-Envision
Copy link

I have been having some trouble getting the source to build locally

What issues? Mind filing another issue for that?

The problems I had were around getting NPM to behave on my machine, so I don't think it is an issue specifically with this project. I had planned on trying to work through it to sort the problem out but I keep getting sidetracked. Will definitely file an issue if I find something and think it is helpful though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants