laravel validation failed code example
Example 1: custom rule laravel validation
public function store()
{
$this->validate(request(), [
'song' => [function ($attribute, $value, $fail) {
if ($value <= 10) {
$fail(':attribute needs more cowbell!');
}
}]
]);
}
Example 2: add error to laravel validation
$validator = \Validator::make(request()->all(), [
'due_date' => 'required',
'template' => 'required'
]);
$validator->after(function ($validator) {
if (request('event') == null) {
$validator->errors()->add('event', 'Please select an event');
}
});
$validator->validate();