Alpine.js Crash Course #7 - Using x-if
Key Takeaways
The video demonstrates how to use the x-if directive in Alpine.js to conditionally output error messages in a form template, allowing users to see which inputs need correction. The tutorial covers creating a function to handle form submissions, validation, and error handling using Alpine.js directives.
Full Transcript
okay then gang in the previous lesson we made a function to handle form submissions and we did some validation on the input values too we also made this errors property which stores error messages for any input fields that failed the validation for example if the username field failed validation we added a username property to the error and if the password failed then we added the password property and so on and all those areas get populated only after we submit the form right so now what I'd like to do is output those areas in the form template if we have any so that a user can see which inputs need correcting and to do this we need to conditionally output the error messages only if they exist on the errors object now when we use Alpine rendering some conditional template is really easy to do by using a directive called xen if but there is a small catch we can't use this directive on any regular HTML element it has to be used on a template element so let me demon this and then we'll talk a little bit more about it so we need a template tag first of all so let's make one of those and then on this template tag we can use the X hyphen if directive to check if a certain condition is true so I could just check to see if we have a value for errors. password for example now if this password property exists on the errors object then this will evaluate as true and Alpine would then inject whatever HTML content was nested in this template into the Dom and then if this ever became false for example if the errors object got reset and the password property no longer existed then Alpine at that moment would remove the nested content directly from the Dom and that is important when we use xive Alpine actively inserts and removes content from the Dom it doesn't just show and hide it there is a different directive called X show which we can use to only show and hide content but we're going to see that later in the course for now let's add some content inside this template tag so the only thing we really want to do is output the error message that we attach right so whatever this is right here and that is on the property itself so errors. password for example so I'm going to create a paragraph tag with a class of error and the reason I give with this class is because it's already styled in styles.css to make it red and then also for this we want Dynamic text so we use the XY text directive and we set it equal to errors do password so whatever that property value is and that's all there is to it so now what I'm going to do is copy this and I'm going to paste it underneath each individual input field so first of all up here the username we need to make sure we update this to be username that's what we're checking for and obviously this as well is username and then down here we have another one for the password the password confirm so let's paste this in and this time it's password confirm and down here password confirm and then we have this one which is the belt color so let me grab that and this and change it to belt and then down here which one's this this is the bio let's do it at the bottom of this and change this and this to bio and then finally down here okay we don't need any error for the newsletter that is just a boo in toggle it's either true or false and there's never any kind of error with it it's either unchecked or checked right so now we can save this and try it out okay so I'm not going to fill anything in and all the validation errors should show except for confirmed password but the rest should show let's submit and yeah we can see those so the username must be at least three characters password six select a belt color bio must be at least 10 okay let's fill some of these in I'm going to say Mario and then down here we'll say test one two for the confirm password I'm just going to say test one and then for the belt color we'll say white let's go for black actually and then say hello I like toast all right then so let us submit this form and now the only error we get is passwords do not match if we add a two on here and click submit all the errors go away and the form gets reset awesome
Original Description
In this Alpine.js tutorial series, you'll learn what Alpine is, and how we can use it to create dynamic, interactive and reactive user interfaces.
🔥🥷🏼Get instant access to ALL premium courses on NetNinja.dev:
https://netninja.dev/
🔥🥷🏼Get instant access to This Alpine Course on NetNinja.dev:
https://netninja.dev/p/alpine-js-crash-course
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/alpine-crash-course
🔗👇 Alpine docs:
https://alpinejs.dev/start-here
🧠🥷🏼HTML & CSS Crash Course:
https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
🧠🥷🏼JavaScript Masterclass:
https://netninja.dev/p/modern-javascript-from-novice-to-ninja
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Why Vanilla JS?
In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach.
https://guseyn.com/html/posts/why-vanilla-js.html
Dev.to · Guseyn Ismayylov
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Medium · JavaScript
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
🎓
Tutor Explanation
DeepCamp AI