LARAVEL Tutorial


Laravel 7 Form Validation Tutorial

Author:- w3alert.com
Updated Date:-11-08-2020

Laravel Forms Tutorial

If you have worked with core PHP with forms, you will be also known about the form in PHP. The laravel also provides great features for form processing.

About Laravel Forms

When you work with laravel forms, you should know about the two basic things of laravel form. The first basic things is csrf token in form and the second one is form method.

The two basic things about laravel forms:

1. CSRF Token:- CSRF Token stands for Cross-Site Request Forgery which is used to protect the spammer in your web application. This is pre-built features of laravel. Therefore, when processing forms, the csrf token must be set. Otherwise, your form is not processed.

2. HTTP Method:- In laravel forms, you can set two methods. The first one is Get And the second one is Post Method. When you process the form in the laravel web application, you must set form methods(GET/POST).

Create basic form in Laravel

Here, you will learn, how to create a basic form in laravel with example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Form</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="{{asset('css/style.css')}}" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
                        <h2>Basic Form in Laravel</h2>
                    </div>
                </div>
            </div>
            <form action="{{ url('save') }}" method="POST">
                @csrf
                <div class="form-group">
                    <label for="name">Name</label>
                    <input class="form-control" name="name" placeholder="Enter name" type="text">
                </div>
                <div class="form-group">
                    <label for="email">Email</label>
                    <input class="form-control" name="email" placeholder="Enter email" type="email">
                </div>
                <button class="btn btn-success" type="submit">
                    <i class="fa fa-save"></i> Submit
                </button>
            </form>         
        </div>
    </div>     
</body>
</html>

Laravel Form With Validation

In laravel form validation, which is simple and secure process to validate form data on serve side. For e.g. any user submit a form in your laravel web application, and his leave some fields blank and post the form. at that time, you need to use laravel form validation rules and return the user on form with error messages.

1. Create form In laravel

Here, you will learn how to create a form in laravel and display the error message if it exist.

You could do like:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Form Validation Example</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="{{asset('css/style.css')}}" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
                        <h2>Laravel Form Validation</h2>
                    </div>
                </div>
            </div>
            <form action="{{ url('save') }}" method="POST">
                @csrf
                <div class="form-group">
                    <label for="username">Name</label>
                    <input class="form-control" name="name" placeholder="Enter name" type="text">
                    @if ($errors->has('name')) 
                    <span class="help-block">{{ $errors->first('name') }}</span>
                    @endif
                </div>
                <div class="form-group">
                    <label for="email">Email</label>
                    <input class="form-control" name="email" value="{{Request::old('email')}}" placeholder="Enter email" type="email">
                    @if ($errors->has('email')) 
                    <span class="help-block">{{ $errors->first('email') }}</span>
                    @endif
                </div>
                <button class="btn btn-success" type="submit">
                    <i class="fa fa-save"></i> Save
                </button>
            </form>         
        </div>
    </div>     
</body>
</html>

Note:- Retrieving and display the error message for a field

To retrieve the first error message for a given field, use the first method:

$errors->has('email'); // Determining If Messages Exist For A Field

echo $errors->first('email'); // To Display error message

2. Create validation Rules On Controller

Here, you need to create validation rules on controller.

You could do like:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    public function index(){

        return view('form');
    }

    public function store(Request $request){
        $request->validate([
            'name' => 'required|string:255',
            'email' => 'required|email',
        ]);

        return redirect('form');
    }
}

You can find all the laravel form validation rules, here https://laravel.com/docs/7.x/validation#available-validation-rules