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

Extending the default form validation #1279

Open
DeanRITS opened this issue Aug 20, 2024 · 0 comments
Open

Extending the default form validation #1279

DeanRITS opened this issue Aug 20, 2024 · 0 comments
Labels
state/sprint-candidate We're trying to get this in a sprint at HQ in the next few weeks

Comments

@DeanRITS
Copy link

DeanRITS commented Aug 20, 2024

Hi,

I am doing a POC with Umbraco Forms and I have a requirement to run some complex client side business rules when a user is moving between pages of a form.

So far, I have overridden the default themes Script.cshtml file and modified it to render a custom JS script which is working.

I am then attempting to run some custom validation when the form is submitted by hooking into the asp-net-validation library as documented here:

https://github.com/haacked/aspnet-client-validation?tab=readme-ov-file#subscribing-to-client-form-validation-event

However, the "validation" event never fires.

I've also tried attaching a "submit" event like below but seem to run into issues:

const formElement = document.querySelector("[data-page-id='abfa8200-c8c7-46b1-a677-b7e50399f6fd']");
formElement.addEventListener("submit", async (e) => {

    const form = e.target;
    e.preventDefault();
    e.stopImmediatePropagation();

    var isValid = window.umbracoFormsValidationService.isValid(form) && runCustomValidation(form);

    if (isValid) {
        form.submit();
        return true;
    }

    return false;
});

1.) If I use preventDefault() and the custom validation fails, my form buttons are disabled.
2.) If I return false without preventing the default event, the form submits anyway.

Can you provide any suggestions or code samples on how correctly hook into the validation pipeline?

Thanks,
Dean


This item has been added to our backlog AB#43271

@BoletteKern BoletteKern added the state/sprint-candidate We're trying to get this in a sprint at HQ in the next few weeks label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state/sprint-candidate We're trying to get this in a sprint at HQ in the next few weeks
Projects
None yet
Development

No branches or pull requests

2 participants