Getting Started With AdonisJS 4.0 [4] - Add Post & Validation

Traversy Media · Beginner ·🛡️ AI Safety & Ethics ·8y ago

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 Changing Your DNS/Nameservers
Changing Your DNS/Nameservers
Traversy Media
2 Create a MySQL database in cPanel
Create a MySQL database in cPanel
Traversy Media
3 Install & Uninstall Joomla Extensions
Install & Uninstall Joomla Extensions
Traversy Media
4 Adding and linking an article in Joomla
Adding and linking an article in Joomla
Traversy Media
5 Create a Joomla Blog
Create a Joomla Blog
Traversy Media
6 Import & Export A MySQL Database
Import & Export A MySQL Database
Traversy Media
7 Use A Custom Font On Your Website Using CSS
Use A Custom Font On Your Website Using CSS
Traversy Media
8 Connect Joomla Site With Dreamweaver
Connect Joomla Site With Dreamweaver
Traversy Media
9 Remove Phoca Gallery 3.2.3 Footer Text
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
10 Drupal 7 Security Update 7.19 to 7.20
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
11 Add An Addon Domain In Cpanel
Add An Addon Domain In Cpanel
Traversy Media
12 Pull A Heroku Rails App and Database
Pull A Heroku Rails App and Database
Traversy Media
13 Create a Custom Joomla 2.5 Module - Part 1
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
14 Create a Custom Joomla 2.5 Module - Part 2
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
15 Create a Custom Joomla 2.5 Module - Part 3
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
16 Joomla SEO Tutorial - sh404sef Configuration
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
17 Font Dragr
Font Dragr
Traversy Media
18 Convert an HTML Template to Joomla 2.5/3.0 - Part One
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
19 Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
20 Rockettheme Rocketlauncher   Joomla Site in Under 10 Minutes
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
21 JQuery FAQ Slider Tutorial
JQuery FAQ Slider Tutorial
Traversy Media
22 301 Redirect With htaccess File
301 Redirect With htaccess File
Traversy Media
23 Convert HTML to Wordpress Theme - Part 1
Convert HTML to Wordpress Theme - Part 1
Traversy Media
24 Convert HTML to Wordpress Theme - Part 2
Convert HTML to Wordpress Theme - Part 2
Traversy Media
25 Easy JQuery Widgets
Easy JQuery Widgets
Traversy Media
26 Codeigniter App Part 1 - Creating the Database
Codeigniter App Part 1 - Creating the Database
Traversy Media
27 Codeigniter App Part 2 - Installation and Configuration
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
28 Codeigniter App Part 6 - Login/Register System
Codeigniter App Part 6 - Login/Register System
Traversy Media
29 Codeigniter App Part 7 - Models List CRUD
Codeigniter App Part 7 - Models List CRUD
Traversy Media
30 Codeigniter App Part 8 - Models Task CRUD
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
31 Node.js Part 1 - Install NodeJS on Windows
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
32 Node.js Part 3 - Building a Static Page Server
Node.js Part 3 - Building a Static Page Server
Traversy Media
33 Node.js Part 4 - NPM
Node.js Part 4 - NPM
Traversy Media
34 Node.js Part 2 - Install MongoDB in Windows
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
35 Create a Joomla Quickstart with Custom Sample Data
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
36 Install MongoDB in Ubuntu
Install MongoDB in Ubuntu
Traversy Media
37 HTML5 Web Storage
HTML5 Web Storage
Traversy Media
38 Create a Joomla Bootstrap Template From Scratch
Create a Joomla Bootstrap Template From Scratch
Traversy Media
39 Ubuntu Server 14.04 Setup Part 1 - Installation
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
40 Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
41 Create A Wordpress Widget - Part 1
Create A Wordpress Widget - Part 1
Traversy Media
42 Create A Wordpress Widget - Part 2
Create A Wordpress Widget - Part 2
Traversy Media
43 Create A Wordpress Widget - Part 3
Create A Wordpress Widget - Part 3
Traversy Media
44 Create A Wordpress Widget - Part 4
Create A Wordpress Widget - Part 4
Traversy Media
45 Get Started With Sass on Windows
Get Started With Sass on Windows
Traversy Media
46 Build An HTML5 Template With Bootstrap and SASS - Part 1
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
47 Build An HTML5 Template With Bootstrap and SASS - Part 6
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
48 Build An HTML5 Template With Bootstrap and SASS - Part 4
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
49 Build An HTML5 Template With Bootstrap and SASS - Part 5
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
50 Build An HTML5 Template With Bootstrap and SASS - Part 3
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
51 Build An HTML5 Template With Bootstrap and SASS - Part 2
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
52 Build An HTML5 Template With Bootstrap and SASS - Part 7
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
53 Build An HTML5 Template With Bootstrap and SASS - Part 10
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
54 Build An HTML5 Template With Bootstrap and SASS - Part 8
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
55 Build An HTML5 Template With Bootstrap and SASS - Part 11
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
56 Build An HTML5 Template With Bootstrap and SASS - Part 9
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
57 Build An Audio Player Using HTML5 & jQuery - Part 1
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
58 Build An Audio Player Using HTML5 & jQuery - Part 2
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
59 Youtube Data API v3 & jQuery To List Channel Videos
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
60 Using Bootstrap With Ruby on Rails
Using Bootstrap With Ruby on Rails
Traversy Media

This video teaches how to add functionality to an AdonisJS form, including submitting data to a MySQL database and implementing form validation. It covers route configuration, controller methods, and session management.

Key Takeaways
  1. Configure routes for form submission
  2. Create a controller method to handle form data
  3. Use the Validator provider for form validation
  4. Set up flash messaging for errors
  5. Implement session management for flash messages
💡 Using the Validator provider simplifies form validation and error handling in AdonisJS applications.

Related Reads

📰
Why American Robots Rebel, Japanese Robots Belong, and Chinese Robots Get the Job Done
Explore how cultural differences in robotics narratives reveal distinct attitudes towards power, personhood, and planning in AI development
Medium · AI
📰
NEWSLETTER: Latest in AI, TikTok, Cybersecurity and “Is AI Entering Its ‘CVE & CVSS’ Moment”
Learn about the latest developments in AI, TikTok, and cybersecurity, and understand the concept of AI's 'CVE & CVSS' moment
Medium · Cybersecurity
📰
Heralding The Minimal Clinically Important Difference When AI Is Used For Human Mental Health
Learn how AI can be used to determine the Minimal Clinically Important Difference (MCID) in human mental health, a crucial technique in the medical field
Forbes Innovation
📰
AI Security Agents Are Moving Beyond Code Review
AI security agents are evolving to focus on exploit-path reasoning, a crucial aspect of cybersecurity, and moving beyond just code review
Medium · Cybersecurity
Up next
VSL International | Build a stronger safety culture through leadership | Bouygues Construction
Bouygues Construction
Watch →