Laravel Validation

Laravel Validation

Validation Quickstart

Form Request Validation

Manually Creating Validators

<?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // Store the blog post...
    }
}

If you do not want to use the validate method on the request, you may create a validator instance manually using the Validator facade.

Working With Error Messages

  • Illuminate\Support\MessageBag.

Available Validation Rules

  • Active_url.
  • After:date.
  • After_or_equal:date.
  • Alpha.
  • Alpha_dash.
  • Alpha_num.
  • Array.
  • Bail.
  • Before:date.
  • Before_or_equal:date.
  • Between:min,max.
  • Boolean.
  • Confirmed.
  • Date.
  • Date_equals:date.
  • Date_format:format.
  • Different:field.
  • Digits:value.
  • .
  • Digits_between:min,max.
  • Dimensions.
  • Distinct.
  • Email.
  • Exists:table,column.

Conditionally Adding Rules

Validating Arrays

$validator = Validator::make($request->all(), [
    'photos.profile' => 'required|image',
]);
$validator = Validator::make($request->all(), [
    'person.*.email' => 'email|unique:users',
    'person.*.first_name' => 'required_with:person.*.last_name',
]);
'custom' => [
    'person.*.email' => [
        'unique' => 'Each person must have a unique e-mail address',
    ]
],

Custom Validation Rules

Laravel Validation — Structure map

Clickable & Draggable!

Laravel Validation — Related pages: