1) Form Request Validation
You may create a "form request" which can hold the authorization logic, validation rules, and error messages for a particular request in your application.
The make:request
Artisan CLI command generates the class and places it in the app/Http/Requests
directory:
php artisan make:request StoreBlogPostRequest
The authorize
method can be overridden with the authorization logic for this request:
public function authorize()
{
return $this->user()->can('post');
}
The rules
method can be overridden with the specific rules for this request:
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
The messages
method can be overridden with the specific messages for this request:
public function messages()
{
return [
'title.required' => 'A title is required',
'title.unique' => 'There is another post with the same title',
'title.max' => 'The title may not exceed :max characters',
'body.required' => 'A message is required',
];
}
In order to validate the request, just type-hint the specific request class on the corresponding controller method. If validation fails, an error response will be sent back.
public function store(StoreBlogPostRequest $request)
{
// validation passed
}
2) Manually Creating Validators
For more flexibility, you may want to create a Validator manually, and handle the failed validation directly:
<?php
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
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...
}
}
2) Fluently creating rules
Occasionally you might need to create unique rules on the fly, working with the boot()
method within a Service Provider might be over the top, as of Laravel 5.4 you can create new rules fluently by using the Rule
class.
As an example we are going to work with the UserRequest
for when you want to insert or update a user. For now we want a name to be required and the email address must be unique. The problem with using the unique
rule is that if you are editing a user, they might keep the same email, so you need to exclude the current user from the rule. The following example shows how you can easily do this by utilising the new Rule
class.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(Request $request)
{
$id = $request->route()->getParameter('user');
return [
'name' => 'required',
// Notice the value is an array and not a string like usual
'email' => [
'required',
Rule::unique('users')->ignore($id)
]
];
}
}