laravel filled validation code example
Example 1: laravel validator make custom message
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.'
];
$this->validate($request, $rules, $customMessages);
Example 2: laravel unique validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 3: laravel validation example
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(['data' => $value],
['data' => 'string|min:1|max:10']
);
if ($validator->fails()) {
}
$validator = Validator::make(['data' => $array],
['email' => 'string|min:1|max:10'],
['username' => 'string|min:1|max:10'],
['password' => 'string|min:1|max:10'],
['...' => '...']
);
if ($validator->fails()) {
}
Example 4: laravel validation
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);