skip to Main Content

I’m starting to create a rest API with Laravel, which I have a question about since before when I did the validations in the controller, I sent it like this

    $rules = [
        'receptor' => 'required',
        'accion' => 'required',
        'state' => 'required',
        'fech_ini' => 'required',
        'fech_fin' => 'required',
    ];
    $message = [
        'receptor.required' => 'Receiver is Required',
        'accion.required' => 'Acción is Required',
        'state.required' => 'State is Required',
        'fech_ini.required' => 'Start Date is Required',
        'fech_fin.required' => 'End Date is Required',
    ];
    $validator = Validator::make($request->all(), $rules,$message);
    if($validator->fails()):
        return back()->withErrors($validator)->with('message','An error has occurred:')->with('typealert','danger')->withInput();
    else:
       //save
    endif;

and in my template blade it showed it like this

<div class="social_media">
  @if (Session::has('message'))
    <div class="container mt-3">
      <div class="alert alert-{{ Session::get('typealert') }}" style="display:none;text-align: left;">
        {{ Session::get('message') }}
        @if ($errors->any())
          <ul>
            @foreach ($errors->all() as $error)
              <li>{{ $error }}</li>
            @endforeach
          </ul>
        @endif
        <script>
          $('.alert').slideDown();
          setTimeout(function(){ $('.alert').slideUp(); }, 10000)
        </script>
      </div>
    </div>
  @endif
</div>

but when making an api it is done differently since the error is sent by a json and in this way I did not get to the answer of how to show the messages on the blade since when consuming the Session api it does not work for me on the blade

$rules = [
    'receptor' => 'required',
    'accion' => 'required',
    'state' => 'required',
    'fech_ini' => 'required',
    'fech_fin' => 'required',
];
$message = [
    'receptor.required' => 'Receiver is Required',
    'accion.required' => 'Acción is Required',
    'state.required' => 'State is Required',
    'fech_ini.required' => 'Start Date is Required',
    'fech_fin.required' => 'End Date is Required',
];
$validator = Validator::make($request->all(), $rules,$message);
if($validator->fails()):
    return response()->json([
      'validator'=> $validator,
      'message'=> 'An error has occurred',
      'typealert' => 'danger',
    ]);
else:
   //save
endif;

2

Answers


  1. Instead of sending back the validator itself in the json response, you could just send back the errors, and specify an http status code to say "this request failed". HTTP 422 is generally used by laravel for validation failures.

    return response()->json([
        'errors'=> $validator->errors()->all(),
        'message'=> 'An error has occurred',
        'typealert' => 'danger',
    ], 422);
    

    With this, you can easily catch the errors when making a request.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search