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

Add support for HTMLFormElement in the validateForm method #403

Open
SofianeLasri opened this issue Nov 15, 2024 · 2 comments
Open

Add support for HTMLFormElement in the validateForm method #403

SofianeLasri opened this issue Nov 15, 2024 · 2 comments

Comments

@SofianeLasri
Copy link

Actually, we have to set a class to the form that we want to validate. This is good for "one time" usage or for validating only the forms we want, but it restricts a little bit the scope.

The idea would be to being able call the validateForm method with the HTMLFormElement instance of the form, just like this :

const forms = document.getElementsByTagName('form');
for (let i = 0; i < forms.length; i++) {
    forms[i].addEventListener('submit', function (e) {
        e.preventDefault();
        let validationStatus = validateForm(this); // Here
        if (validationStatus) {
            this.submit();
        }
    });
}

With that, we would be able to fully 'automatize' the forms validation.

@mkocansey
Copy link
Owner

Sure @SofianeLasri this can be done and I'll add it to the next update. Just curious though. Are there use-case where you will have multiple forms on a page that all need to be validated at once on click of one button?

@SofianeLasri
Copy link
Author

SofianeLasri commented Nov 15, 2024

No I actually don't have one for this use case. But, my idea was to add this piece of code inside my view layout. Here, it is for my authenticaton pages. But I think that I will do the same for the dashboard layout were almost all the pages will contains forms.

<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@yield('title', 'Authentification')</title>
    <link href="{{ asset('vendor/bladewind/css/animate.min.css') }}" rel="stylesheet"/>
    <link href="{{ asset('vendor/bladewind/css/bladewind-ui.min.css') }}" rel="stylesheet"/>
    <script src="{{ asset('vendor/bladewind/js/helpers.js') }}"></script>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
<div class="min-h-screen flex flex-col justify-center items-center bg-gray-100">
    <x-bladewind.card class="w-full max-w-md">
        @yield('content')
    </x-bladewind.card>
</div>
<script type="text/javascript">
    dom_el('.validate-me').addEventListener('submit', function (e) {
        e.preventDefault();
        let validationStatus = validateForm('.validate-me');
        if (validationStatus) {
            this.submit();
        }
    });
</script>
</body>
</html>

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

No branches or pull requests

2 participants