Diwali Offer : Ecommerce Website + Android App (70% Off)

how to use form validation in laravel

Deepak Tailor Image
Deepak Tailor - Jan 24 2023
how to use form validation in laravel

In Laravel, we check the form data to prevent garbage values, we call it form validation. We validate the form before saving the data in the controller file to the database. Laravel has provided validate function for data validation.

Why use form validation in laravel

Form validation is a necessary process before saving the data. By which we remove the error value or dummy value. When validated, we get a unique data.

Mostly used form validation rules in laravel

Some rules are mostly used like required, email, min, max, unique, enum, numeric etc.

required - value must not be empty

email - value must be of type email

min - the value must be up to this minimum length

max - the value must be up to this maximum length

numeric - value must be numeric

Validate form data in laravel

Create register form in blade file

<form action="/register" method="POST">
    @csrf
    <div class="form-group">
        <label for="">
            Full Name
        </label>
        <input class="form-control" name="full_name" placeholder="Enter Name" type="text">
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Email address
        </label>
        <input class="form-control" name="email" placeholder="Enter email" type="email">
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Password
        </label>
        <input class="form-control" name="password" placeholder="Password" type="password">
        </input>
    </div>
    <button class="btn btn-primary" type="submit">
        Submit
    </button>
</form>

Create controller for validate form data

public function register(Request $request){
    $request->validate([
        'full_name' =>  'required',
        'email'     =>  'required|email',
        'password'  =>  'required|min:6'
    ]);
}
Show validation errors in blade files

Show all errors with all function in errors variable

@php
print_r($errors->all())
@endphp

Show specific error

<form action="/register" method="POST">
    @csrf
    <div class="form-group">
        <label for="">
            Full Name
        </label>
        <input class="form-control" name="full_name" placeholder="Enter Name" type="text">
            @error('full_name')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Email address
        </label>
        <input class="form-control" name="email" placeholder="Enter email" type="email">
            @error('email')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Password
        </label>
        <input class="form-control" name="password" placeholder="Password" type="password">
            @error('password')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <button class="btn btn-primary" type="submit">
        Submit
    </button>
</form>
how to fix show form data after submit form

Page refresh but remove form data in blade file . The form data becomes empty when the form is submitted. For this, Laravel provides us old function, so that the form data is shown in the form even after submission.

<form action="/register" method="POST">
    @csrf
    <div class="form-group">
        <label for="">
            Full Name
        </label>
        <input class="form-control" name="full_name" placeholder="Enter Name" type="text" value="{{ old('full_name') }}">
            @error('full_name')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Email address
        </label>
        <input class="form-control" name="email" placeholder="Enter email" type="email" value="{{ old('email') }}">
            @error('email')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <div class="form-group">
        <label for="">
            Password
        </label>
        <input class="form-control" name="password" placeholder="Password" type="password" value="{{ old('password') }}">
            @error('password')
            <span class="text-danger">
                {{ $message }}
            </span>
            @enderror
        </input>
    </div>
    <button class="btn btn-primary" type="submit">
        Submit
    </button>
</form>
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in