How to Validate a Form in Laravel?

Laravel provides a lot of ease out of the box when it comes to validation. Let me show you in code. Let’s say we want to add a user in our website. So we need a form. Create add_user.blade.php in your views folder.

<!DOCTYPE html>
<html>
<head>
<title>Laravel form validation example - Laramatic.com</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">

    <h1>Laramatic - Laravel form validation example</h1>

    @if(Session::has('success'))
        <div class="alert alert-success">
            {{ Session::get('success') }}
            @php
                Session::forget('success');
            @endphp
        </div>
    @endif

    <form method="POST" action="{{ route('user.post') }}">

        @csrf

        <div class="form-group">
            <label>Name:</label>
            <input type="text" name="name" class="form-control" placeholder="Name">
            @if ($errors->has('name'))
                <span class="text-danger">{{ $errors->first('name') }}</span>
            @endif
        </div>

        <div class="form-group">
            <label>Password:</label>
            <input type="password" name="password" class="form-control" placeholder="Password">
            @if ($errors->has('password'))
                <span class="text-danger">{{ $errors->first('password') }}</span>
            @endif
        </div>

        <div class="form-group">
            <strong>Email:</strong>
            <input type="text" name="email" class="form-control" placeholder="Email">
            @if ($errors->has('email'))
                <span class="text-danger">{{ $errors->first('email') }}</span>
            @endif
        </div>

        <div class="form-group">
            <button class="btn btn-success btn-submit">Submit</button>
        </div>
    </form>
</div>
</body>
</html>

Now you need to add these routes.

Route::get( '/create-user', 'HomeController@create' )->name( 'user.create' );
Route::post( '/post-user', 'HomeController@store' )->name( 'user.post' );

and in HomeController you need to add following functions.

public function create() {
		return view( 'add_user' );
	}

	public function store( Request $request ) {
		$request->validate( [
			'name'     => 'required',
			'password' => 'required|min:5',
			'email'    => 'required|email|unique:users'
		], [
			'name.required'     => 'Name is required',
			'password.required' => 'Password is required'
		] );

		$input             = $request->all();
		$input['password'] = \Illuminate\Support\Facades\Hash::make( $input['password'] );
		$user              = User::create( $input );

		return back()->with('success', 'User created successfully.');

	}

Now visit /create-user on your site. You can add users with validation. Laravel validations are very easy to implement.

We just need to use pipe notation and we can validate anything. For example, we can validate email by using this.

'email' => 'required|email|unique:users'

Now there are many validation rules out of the box which we can use. For example, for start date validation we can easily implement our rule like this.

'start_date' => 'required|date|after:tomorrow'

and for finish date.

'finish_date' => 'required|date|after:start_date'

for avatar image rule is very easy as following.

'avatar' => 'dimensions:min_width=100,min_height=200'

and even if you want to implement rule for dimension.

'avatar' => 'dimensions:ratio=3/2'

to implement rule on something ignoring case sensitivity we can do this.

'foo.*.id' => 'distinct:ignore_case'

I hope now form validation is very easy for you. Visit Laravel official documentation for validation for more details and if you are finding it difficult please use comments section with your code. We appreciate that.