Example 1: enie letter validation laravel regex
return [
'fname' => 'required|max:255|regex:/^[a-zA-ZÑñ\s]+$/',
'lname' => 'required|max:255|regex:/^[a-zA-ZÑñ\s]+$/',
'mname' => 'max:255|alpha',
'file' => 'image|mimes:jpg,jpeg,png',
'contact_no' => 'regex:/^[-0-9\+]+$/',
'date_of_birth' => 'required|date_format:Y-m-d',
'school_id' => 'required|exists:schools,id',
'degree_id' => 'required|exists:degrees,id',
];
Example 2: 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 3: if any error in blade laravel
@if ($errors->any())
@foreach ($errors->all() as $error)
<div>{{$error}}</div>
@endforeach
@endif
Example 4: 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 5: laravel validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 6: laravel unique validation
unique:table,column,except,idColumn
############## Example : ################
############### For Updating
'email' => 'unique:users,email_address,' . $userId,
############### For Creating
'email' => 'unique:users,email_address',