Skip to content

Remote validator strange behavior #14

@mind-ra

Description

@mind-ra

I encountered a strange behavior for the Remote validator.

After the remote validator fire, regardless of the result, the form is posted even if the form is invalid.

It seem the preventDefault and the stopPropagation of the submit event is skipped, but I cannot understand how to fix it.

@page
@model IndexModel
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <form method="post">
        <label asp-for="Id"></label>
        <input asp-for="Id" />
        <span asp-validation-for="Id"></span>

        <label asp-for="Control"></label>
        <input asp-for="Control" />
        <span asp-validation-for="Control"></span>

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

    <script src="aspnet-validation.js"></script>
    <script>
         const service = new aspnetValidation.ValidationService();
        service.bootstrap();
    </script>
</body>
</html>
public class IndexModel : PageModel
{
    [BindProperty]
    [Required]
    [Remote("CheckRemote", "Validations", HttpMethod = "Post")]
    public string Id { get; set; }
    [BindProperty]
    [Required]
    public string Control { get; set; }

    public IActionResult OnPost()
    {
        if (!ModelState.IsValid)
        {
            return BadRequest();
        }
        return Content("OK");
    }
}

[ApiController]
[Route("api/[controller]")]
public class ValidationsController : ControllerBase
{
    [HttpPost]
    [Route("check-remote")]
    public IActionResult CheckRemote()
    {
        bool isValid = false;
        return Ok(isValid);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions