I would like to know when a validation failed by using this kind of code writing (I'm using laravel 5.4)
$this->validate($request, [
'name' => 'required|min:2|max:255'
]);
I know that I can use this:
$validator = Validator::make($request->all(), [
'name' => 'required|min:2|max:255'
]);
if ($validator->fails()) { //Not okay }
But I would like to keep this way of validating by using $this->validate
instead of using the Validator
model.
So ... is it possible to use something like:
//This is not working btw
$test = $this->validate($request, [
'name' => 'required|min:2|max:255'
]);
if( $test )
{ //Ok }
else
{ //Not okay };
You can use it like this:
$request->validate($rules);
or
$request->validate([
'name' => 'required|min:2|max:255'
]);
Then it returns the errors.
$test = $request->validate([
'name' => 'required|min:2|max:255'
]);
and you need to check if there are no errors and then you can do what ever you want.
In your case you need to do it like this:
$validator = Validator::make($request->all(), [
'name' => 'required|min:2|max:255'
]);
if ($validator->fails()) {
return view('view_name');
} else {
return view('view_name');
}