Getting Started With AdonisJS 4.0 [4] - Add Post & Validation
Key Takeaways
This video tutorial demonstrates how to add functionality to an AdonisJS form, including submitting data to a MySQL database, setting up flash messaging for errors, and implementing form validation using the Validator provider.
Full Transcript
alright guys so we have our ad post form now we need to add function to this so it's getting submitted to slash posts as a post request so we need to catch this in our routes so let's go to start route CAS and we'll go down here and we'll say route dot post because it's gonna be a post request and it's gonna be two slash posts and then we want to link this to the post controller dot create method which we haven't created yet so let's save that actually you know what let's call this store I think that's a better convention so we're storing the post all right now let's go to the controller and we'll go right on it under the add method and we'll say async store and let's see what we're gonna do here so this is gonna take in a couple things okay we want request and response and then we also want session alright we're going to be using flash messages so that when we submit a post after we redirect we want a message that says post submitted or something like that all right we're also gonna be using validation using a validated provider but I'm gonna get to that after so once this is submitted let's say Const Post equals new post like that okay so we're creating a new post instance and then let's say post dot title equals and the way that we get the form values is using that request object which has an input method and we can get the title okay we also want to get the body and add that to the post so request input body all right then we just want to save it now we need to use a weight here so we're gonna say a weight post dot save okay now we want to set a flash message so to do that we use session dot flash and then in here we pass in an object and we say notification and then whatever we want to say I'm gonna say post added okay and then we just need to return response dot redirect and wherever we want to go which is gonna be the posts page all right now setting the flash message is one thing but we need we also need to output it so since we're going to the post index page let's go to that view which is post index dot edge and we're gonna go right below the title and the syntax for this is going to be an if statement so we want to do a tiff and in here we want to say old that's the method we want to run and then notification okay and then we want to end if and inside here we're going to put a div with a class of alert and a class of alert danger okay which are bootstrap classes and then we just want to put double curly braces old notification like that all right so let's try this it should actually add a post so we'll reload let's say post three this is post three and submit and there we go post added oops I I put alert danger I should have this needs to be alert success so it's green okay so one thing that I want to do now is add some validation because right now I can submit nothing okay so to do this we need to install the validator providers so we're gonna go down here and say npm install at adonis jas slash validator okay this isn't included by default now when we add a provider we need to register it and we do that in the start and then app.js file so i'm what i'm gonna do is put a comma here and let's just copy this down and change this to validate or provider okay so now we can use it so we'll close this up let's go back to our controller and we need to bring in that validator so up at the top here we're gonna say const validate equals actually we need to use destructuring so we need to add curly braces around that and then we'll set that to use validator look I put some semicolons in here I knew that was gonna happen all right so now we brought in that validator provider so now we want to use it so down in the store method we're gonna go to the very top before we do anything with the database and we need to add our validation so we'll say Const validation set that to a weight validate and we want to pass in request dot all and after that request on all method we want to put a comma and then open up some curly braces and put in our fields so title and then the rules we want so I'm gonna say required and then we can separate rules with pipes I'm gonna say minimum of three characters another pipe and a maximum of 255 characters all right and then for the body I'm just gonna say required and men three all right just like that so that defines our rules now we need to check to see if that fails so we're gonna say if validation dot fails okay if validation fails then we want to session dot with errors and then pass in here validation dot messages and then we want to dot flash all alright so each each field will have its own little flash message okay then we just want to return we want to reload the same page so response dot redirect and back okay which will just reload the same page the form so that looks good let's go ahead and save that now if we were to go over here and try to oh what happened let's see what happened cannot find provider hmm should did I not save it let's see let's go back to start app J s oh I have auth providers here this should actually be validator all right save that that should fix it good so now if I try to submit without entering any any data it's not letting me but the messages are not showing so we need to go back to add dot edge and we need to add some stuff here okay and some of this looks a little weird but it's just what we have to do so first of all we need to put a value so value for title and this is gonna be double curly braces and then again we want that old method passing the name of the field which is title and then an empty string so what this will do is if there are errors and we have something in here it'll make the con it'll make the text stay in there all right now we want to do the same for the body so I'm going to copy the the curly braces and put it inside here and then just change that to body all right now to show the actual errors we're going to go to this first title input go right underneath it and we're gonna use this L if syntax alright so we need e l uppercase I F okay and then here we're gonna put in whatever we want to display the error so I'm going to use a span with the class of text - danger okay that'll make the that'll make the text read span and inside the span we want money signed self alright now we want to go to the end here and put in another parameter which is going to be get error for so get our four and then we'll pass in the name of the field which is title alright then we want to pass in another parameter of has error four so has error four and pass in title okay and that will display the error now we want to do the same thing for the body so I'm gonna copy this go right under the text area paste that in and just change these two to body so now let's go ahead and reload and let's try to submit we get required validation failed on title I put something in there and submit required validation on body and notice that this stays in there that's because of this value we put here so let's go ahead and add another post we'll say post for this is post four submit and there we go so now we have a little bit of form validation alright so in the next video we're gonna get to update these posts we want to add that we want an edit button where we can click edit go to a form update it and then we also want to add a delete button so that we can delete posts
Original Description
In this video we will add functionality to the add post form and insert data into MySQL. We will also install the validator provider for form validation as well as setup the flash messaging for errors
CODE: Github Repo
https://github.com/bradtraversy/adonis40blog
BECOME A PATRON: Show support & get perks!
http://www.patreon.com/traversymedia
ONE TIME DONATIONS:
http://www.paypal.me/traversymedia
VISIT MY WEBSITE:
http://www.traversymedia.com
FOLLOW TRAVERSY MEDIA:
http://www.facebook.com/traversymedia
http://www.twitter.com/traversymedia
http://www.instagram.com/traversymedia
https://discord.gg/traversymedia
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Traversy Media · Traversy Media · 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
Changing Your DNS/Nameservers
Traversy Media
Create a MySQL database in cPanel
Traversy Media
Install & Uninstall Joomla Extensions
Traversy Media
Adding and linking an article in Joomla
Traversy Media
Create a Joomla Blog
Traversy Media
Import & Export A MySQL Database
Traversy Media
Use A Custom Font On Your Website Using CSS
Traversy Media
Connect Joomla Site With Dreamweaver
Traversy Media
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
Add An Addon Domain In Cpanel
Traversy Media
Pull A Heroku Rails App and Database
Traversy Media
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
Font Dragr
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
JQuery FAQ Slider Tutorial
Traversy Media
301 Redirect With htaccess File
Traversy Media
Convert HTML to Wordpress Theme - Part 1
Traversy Media
Convert HTML to Wordpress Theme - Part 2
Traversy Media
Easy JQuery Widgets
Traversy Media
Codeigniter App Part 1 - Creating the Database
Traversy Media
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
Codeigniter App Part 6 - Login/Register System
Traversy Media
Codeigniter App Part 7 - Models List CRUD
Traversy Media
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
Node.js Part 3 - Building a Static Page Server
Traversy Media
Node.js Part 4 - NPM
Traversy Media
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
Install MongoDB in Ubuntu
Traversy Media
HTML5 Web Storage
Traversy Media
Create a Joomla Bootstrap Template From Scratch
Traversy Media
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
Create A Wordpress Widget - Part 1
Traversy Media
Create A Wordpress Widget - Part 2
Traversy Media
Create A Wordpress Widget - Part 3
Traversy Media
Create A Wordpress Widget - Part 4
Traversy Media
Get Started With Sass on Windows
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
Using Bootstrap With Ruby on Rails
Traversy Media
More on: Database Integration
View skill →Related Reads
📰
📰
📰
📰
Why American Robots Rebel, Japanese Robots Belong, and Chinese Robots Get the Job Done
Medium · AI
NEWSLETTER: Latest in AI, TikTok, Cybersecurity and “Is AI Entering Its ‘CVE & CVSS’ Moment”
Medium · Cybersecurity
Heralding The Minimal Clinically Important Difference When AI Is Used For Human Mental Health
Forbes Innovation
AI Security Agents Are Moving Beyond Code Review
Medium · Cybersecurity
🎓
Tutor Explanation
DeepCamp AI