Validation does not return array with errors

Asked

Viewed 222 times

1

I’m trying to use the validation of the Laravel to validate a form, but I’m not getting the array of errors

Controller

use Illuminate\Http\Request;
class Curriculos extends Controller {
public function store(Request $request){

    $this->validate($request, [
      'nome' => 'required|min:5'
    ]);

    $curriculos = $request->all();

    $curriculos['dt-nascimento'] = str_replace('/', '-', $curriculos['dt-nascimento']);
    $curriculos['dt-nascimento'] = date('Y-m-d', strtotime($curriculos['dt-nascimento']));

    $curriculos['dt-inicio'] = $this->formatDate($curriculos['dt-inicio']);

    $curriculos['dt-conclusao'] = $this->formatDate($curriculos['dt-conclusao']);

    $curriculo = new Curriculo;

    if($curriculo->create($curriculos)){
      return redirect('/');
    }


}

View

 <?php var_dump($errors->all()); ?>
 <form class="" action="{{url('/api/send')}}" method="post">
   @csrf
  ......

Result of Var_dump()

 array(0) { } 
  • Where is this route set in web.php or api.php

  • In api, as Post

1 answer

2


When using routes defined in api.php, as referred by Luhan Salimena is not applied the middleware group Shareerrorsfromsession (see configuration in Kernel.php).

Without application of this middleware group the variable $errors is not passed to the View

See the documentation on https://laravel.com/docs/5.6/validation

The routes in api.php are suitable for requests via Ajax in this case by default when there is a validation error will get a Json response with code 422 with all errors found.

I suggest you change your route to web.php which seems to be more appropriate to your case.

  • It worked here, thank you very much :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.