GithubHelp home page GithubHelp logo

hans-lts / alpine-validation-example Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 264 KB

A small sample showing how to share Livewire validation states with AlpineJS components.

PHP 92.42% CSS 0.58% JavaScript 0.53% Blade 6.47%

alpine-validation-example's Introduction

Sharing Livewire validation data with AlpineJS

I build this project to be a proof of concept for sharing Livewire validation state with AlpineJS components. .env and prebuilt assets are included to ease the project startup process, feel free to change as needed.

Installation

Clone the repo to a local folder and install the composer dependencies.

> composer install

Next, install the NPM dependencies

> npm i
> npm run build

Lastly, start up the Laravel server and access the site from your browser.

> php artisan serve

  INFO  Server running on [http://127.0.0.1:8000].

  Press Ctrl+C to stop the server

Overview

The local component demonstrates a Livewire component tracking and handling its own error states. There is a 'global' bit of code that is used to dispatch the error messages to the proper components, however.

Here's the meat of it:

document.addEventListener("DOMContentLoaded", () => {
    Livewire.hook('message.processed', (message, component) => {
        let alpineComponent = Alpine.$data(component.el);

        // Make sure we aren't going to call an invalid alpine component
        if(alpineComponent.processValidation) {
            alpineComponent.processValidation(message.response.serverMemo.errors);
        }
    })
});

Reference: Livewire docs - Lifecycle Hooks

It's pretty simple, actually. This block of code will catch Livewire messages after they've been processed forward the message to the corresponding component with an updated validation state. If the errors are empty, there's no validation errors.

In the Livewire component, we've added the x-wire-errors attribute which enables all the features from the package. You can read what's available at the repo's README.

The real important bit is to set the listener on the root Livewire element. Like so:

<div wire:id="wIhowMna8Zh2UxDqhWeq" x-data x-wire-errors >
    <~-- The rest of the component -->
</div>

In the demo, the Age field will be evaluated after the user blurs the input and a message will be displayed next to the input field label.

alpine-validation-example's People

Contributors

hans-lts avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.