colinaut / alpinejs-plugin-simple-validate

Simple Alpine form validation plugin
97 stars 4 forks source link

Feature request: automatic validation on submit #19

Closed 64knl closed 11 months ago

64knl commented 1 year ago

I'm trying to run this script under a strict Content Security Policy and cannot get @submit="$validate.submit" to work. Not sure if it's me doing something wrong, or CSP version of Alpine just doesn't allow it.

It would be nice if there was an option to automatically trigger validation from within the plugin. This could be triggered by a CSP safe option such as an extra attribute on the <form> tag.

Somewhat working sample:

// Can be added after: el.setAttribute("novalidate", true);
if ( theOptionToAutomaticallyValidate === true ) 
{
  el.addEventListener("submit", function (e) {
    validateMagic.submit(e);
  });
}

To reproduce:

  1. Compile CSP version of Alpine (https://alpinejs.dev/advanced/csp)
  2. Create form:
<form
            action="/target-url"
            method="POST"
            x-data
            x-validate.input
            @submit="$validate.submit"
>
  1. Submitting the form will not trigger the validation.
  2. After modifying the plugin as shown above validation will work.
colinaut commented 11 months ago

Hrm yeah I haven't tried it with CSP. You're request was simple so I added it. Sorry for the delay in responding, but the past couple months have been really busy.

With the new 1.7.22 you can add x-validate.validate-on-submit to the form element to trigger it. Hope this works for you.