laravel validation text code example
Example 1: displaying errors in laravel
@if(count($errors) > 0)
<div class="p-1">
@foreach($errors->all() as $error)
<div class="alert alert-warning alert-danger fade show" role="alert">{{$error}} <button type="button" class="close"
data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button></div>
@endforeach
</div>
@endif
Example 2: how to add an custom error to validater error in laravel
if (request('event') == null) {
$validator->errors()->add('event', 'Please select an event');
}
Example 3: laravel validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
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
"foo" => "Your input was invalid!",
"accepted" => "The :attribute must be accepted.",