Hazem Alrfati
Hazem Alrfati

Reputation: 47

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

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")}}" class="mt-5 w-50 m-auto">
    @csrf
  <div class="mb-3">
    <label class="form-label">Movie Name</label>
    <input type="text" name="movie_name" class="form-control">
    @error('movie_name')
    <span class="error">{{$message}}</span>
    @enderror
  </div>
  <div class="mb-3">
    <label class="form-label">Movie Descrption</label>
    <input type="text" name="movie_description" class="form-control">
    @error('movie_description')
    <span class="error">{{$message}}</span>
    @enderror
  </div>
  <div class="mb-3">
    <label class="form-label">Movie Gener</label>
    <input type="text" name="movie_gener" class="form-control">
    @error('movie_gener')
    <span class="error">{{$message}}</span>
    @enderror
  </div>
  <button type="submit" name="action" value="back" class="btn btn-warning me-3">Back</button>
  <button type="submit" name="action" value="add" class="btn btn-primary">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");
    }
}

Upvotes: 0

Views: 745

Answers (2)

Bhola Kr. Khawas
Bhola Kr. Khawas

Reputation: 385

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')}}" class="btn btn-warning me-3">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.

Upvotes: 1

Angad Dubey
Angad Dubey

Reputation: 5452

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");
    }
}

Upvotes: 0

Related Questions