Reputation: 453
I have a form in laravel 5 with all field being required. If the user doesn't input anything in the field, an error messages will be shown just under that field. The error message is shown like this:
{!!$errors->first('Name','<div class="has-error"><span></span>:message</div>')!!}
...to give you an example, if the user doesn't input anything in the field "Name", the message will be: "The name field is required."
What I want is to capture this message in a variable, something like:
$mess=something;
..i need this something...
If i do echo $mess
the result should be The name field is required.
Can you help me please ? Thanks.
Upvotes: 0
Views: 525
Reputation: 6091
This sounds like you want to capture the errors in the controller directly, right? Because you are using $this->validate()
in your controller, thus returning you the errors and you can access is with the $errors
variable. Is this correct so far?
If yes, then don't use $this->validate()
, but this instead
$v = Validator::make($request->all(), [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
if ($v->fails()) {
return view('viewname', ['mess' => $v->errors()]);
}
This lets you save the Validator
instance in a variable, and do the check manually. $v->errors()
now contains all your errors, which you can return to your view as mess
.
Upvotes: 1