Laravel 8 tutorial | Ajax Form validation and display errors on blade without page refresh

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends in this video tutorial we are going to show you how you can make validation in laravel 8 with ajax this is the best way to display errors from controller under each element on blade file without page refresh with ajax let's get started modified.env file by changing db underscore database equal to ajax underscore validation under score laravel then navigate phpmyadmin and create database with name ajax underscore validation under score laravel open terminal and enter php artisan serve command to run our application as you can see we are on laravel 8. let's create student controller by entering php artisan make colon controller student controller [Music] then create index function under student controller class import student controller on web.php by typing use space app backslash http backslash controllers backslash student controller line then create root for index method create view for this root this page will contain our form link bootstrap on this page also add jquery library to this page let's check if jquery works or not good our jquery works perfectly let's continue to create our ui by using bootstrap classes my [Music] our form will have name email and password input fields as you can see our ui has been completed let's give our form id to main underscore form let's try to submit form by using jquery and prevent page to be refreshed as you can see our page not reloaded to create clean code let's use main.js file that will contains all js code let's write codes to submit form via ajax these codes will submit form data to server without page refresh let's create save root then use this root name to our form action attribute and don't forget to add csrf on this form in order to display error under each element let's add span on each element where error message will be displayed make sure if this prefix is equal to the element name value so let's create save method under student controller class let's first import validator class on this controller then start validate our requested form inputs [Music] name will be required email will be required and be in email format and password will be required then create student table manually in order to use query builder let's import db class on this controller by typing use space illuminate backslash support backslash facades backslash db line for this if statement will show us if our form not validated so if we get validation errors let's return errors to be displayed on blade if we don't have any error validation then insert data into database if data have been inserted successfully then display success message so how to get errors from controller via ajax on before send function let's make all spans with error text class to have empty text [Music] then on success function let's check if we get errors from controller check if we get status equal to zero so if our status equal to zero let's display error on each element span text this prefix will be this then if we get status equal to 1 let's reset our form and display success message alert let's test as you can see are errors displayed on each element span so our data have been successfully inserted into database via ajax without page refresh you can check if email is unique then password must have minimum length of 5 and maximum length of 12. let's see if this validation error will be displayed on blade [Music] as you can see we have an error show us the email already taken then on password we have also error show us required minimum and maximum length for using hashed password let's import hash class by entering use space illuminate backslash support backslash facades backslash hash line as you can see password has been hashed friends this is how you can make validation and display error without page refresh via ajax in laravel if you are new on this channel please subscribe today in order to receive notification that inform you if we uploaded new video thanks for watching this video
Info
Channel: Irebe Library
Views: 8,650
Rating: undefined out of 5
Keywords: irebe library, ajax validation, laravel validation, laravel 8 validation, laravel 8 ajax validation, laravel 8, laravel, laravel 8 form validation, Ajax Form validation and display errors on blade without page refresh, laravel tutorial, laravel 8 tutorial, make jquery validation in laravel 8, laravel form validation with ajax
Id: Pij6bn0ebLo
Channel Id: undefined
Length: 18min 51sec (1131 seconds)
Published: Mon Dec 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.