Home > Blockchain >  Laravel 8 - How to suppress validation errors with a back button in Laravel
Laravel 8 - How to suppress validation errors with a back button in Laravel

Time:12-20

How to suppress validation errors with a back button in Laravel ??

Details: I have form with two buttons and I made validation how to skip validation when click on (back-button)?

the code in the view page:

<form method="POST" action="{{route("movies.store")}}" >
    @csrf
  <div >
    <label >Movie Name</label>
    <input type="text" name="movie_name" >
    @error('movie_name')
    <span >{{$message}}</span>
    @enderror
  </div>
  <div >
    <label >Movie Descrption</label>
    <input type="text" name="movie_description" >
    @error('movie_description')
    <span >{{$message}}</span>
    @enderror
  </div>
  <div >
    <label >Movie Gener</label>
    <input type="text" name="movie_gener" >
    @error('movie_gener')
    <span >{{$message}}</span>
    @enderror
  </div>
  <button type="submit" name="action" value="back" >Back</button>
  <button type="submit" name="action" value="add" >Add</button>
</form>

the code in the controller file:

public function store(MoviesFormRequest $request)
{

    switch ($request->input('action')) {
        case 'back':
            return redirect()->route("movies.index");

        case 'add':
            $data = $request->validated();

            Movie::create($data);

            return redirect()->route("movies.index");
    }
}

CodePudding user response:

If you just want to solve the problem by implementing back button instead of using button tag use anchor tag as follows

<a href="{{rout('movies.index')}}" >Back</a>

And remove the switch statement and just do as follows:

public function store(MoviesFormRequest $request)
{
    Movie::create($request->all());

    return redirect()->route("movies.index");
}

Inside your MoviesFormRequest class do all the validations like :

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MoviesFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
      */
    public function rules()
    {
        return [
            'movie_name' => 'required',
            'movie_description' => 'required',
            'movie_gener' => 'required',
        ];
    }
}

This will work for you just customize the anchor tag to look good. Hope this solve your problem.

CodePudding user response:

The validation happens in your custom request (MoviesFormRequest) before the code in the controller method is executed.

So in order to skip validation given a specific request input, you have to make the it part of the switch block

use Illuminate\Http\Request;

// use the base request here (no validation at this point)
public function store(Request $request)
{

    switch ($request->input('action')) {
        case 'back':
            return redirect()->route("movies.index");

        case 'add':
            // in our case block we can validate
            $this->validate($request, [ 
                'title' => ['required'],
                //... your rules here
            ]);

            $data = $this->validated();

            Movie::create($data);

            return redirect()->route("movies.index");
    }
}
  • Related