Laravel 7 Form Validation Example Tutorial – onlinecode
In this post we will give you information about Laravel 7 Form Validation Example Tutorial – onlinecode. Hear we will give you detail about Laravel 7 Form Validation Example Tutorial – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Today, We explain to you and how to create step by step form validation example in laravel 7.
Here we are using all and has methods for form validation. if you want to customize an error message, then you can use the below example through you can make it.
So you can follow the below step.
Overview
Step 1: Create RoutesStep 2: Create a Model and ControllerStep 3: Create Blade FilesStep 4: Run Our Laravel Application
Step 1: Create RoutesAdd the following route code in the “routes/web.php” file.
Route::get('student', 'StudentController@create'); Route::post('student', 'StudentController@store');
Step 2: Create a Model and ControllerHere below command help to create the controller and model.
php artisan make:controller StudentController --resource --model=Student
Student.php
<?php namespace App; use IlluminateDatabaseEloquentModel; class Student extends Model { protected $fillable = [ 'first_name','last_name', 'address' ]; } ?>
StudentController.php
<?php namespace AppHttpControllers; use AppStudent; use IlluminateHttpRequest; class StudentController extends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { // $students = Student::all(); return view('student.list', compact('students','students')); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { // return view('student.create'); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { // $request->validate([ 'txtFirstName'=>'required', 'txtLastName'=> 'required', 'txtAddress' => 'required' ], [ 'txtFirstName.required' => 'FirstName is required', 'txtLastName.required' => 'LastName is required', 'txtAddress.required' => 'Address is required' ]); $student = new Student([ 'first_name' => $request->get('txtFirstName'), 'last_name'=> $request->get('txtLastName'), 'address'=> $request->get('txtAddress') ]); $student->save(); return redirect('/student')->with('success', 'Student has been added'); } } ?>
Step 3: Create Blade FilesSo finally, first we will create the new directory “resources/views/student/layouts” and that directory in create a “resources/views/student/layouts/app.blade.php” file. and the second time we will create a list.blade.php in the “resources/ views/student/” directory.layout.blade.php
<!DOCTYPE html> <html lang="en"> <head> <title>laravel 7 form validation Example Tutorial - onlinecode</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div > @yield('content') </div> </body> </html>
create.blade.php
@extends('student.layouts.app') @section('content') <div > <div > <h2>Add New Student</h2> </div> <div > </div> </div> @if ($errors->any()) <div > <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('student.store') }}" method="POST"> @csrf <div > <label for="txtFirstName">First Name:</label> <input type="text" id="txtFirstName" placeholder="Enter First Name" name="txtFirstName"> @if ($errors->has('txtFirstName')) <span >{{ $errors->first('txtFirstName') }}</span> @endif </div> <div > <label for="txtLastName">Last Name:</label> <input type="text" id="txtLastName" placeholder="Enter Last Name" name="txtLastName"> @if ($errors->has('txtLastName')) <span >{{ $errors->first('txtLastName') }}</span> @endif </div> <div > <label for="txtAddress">Address:</label> <pre id="txtAddress" name="txtAddress" rows="10" placeholder="Enter Address"></pre> @if ($errors->has('txtAddress')) <span >{{ $errors->first('txtAddress') }}</span> @endif </div> <button type="submit" >Submit</button> </form> @endsection
Step 4: Run Our Laravel ApplicationWe can start the server and run this example using the below command.
php artisan serve
Now we will run our example using the below Url in the browser.
Hope this code and post will helped you for implement Laravel 7 Form Validation Example Tutorial – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs