Search code examples
laravellaravel-validation

Can't open form that uses form request validation in Laravel


I can't open open 2 forms (create, edit) that are validated by the same form request class.

When I click on the create or edit button, I just get a redirect to the same page with 302 Found status code visible in Dev Tools, without form being open.

When I remove form request validation from the methods in a controller, I can open forms normally.

Controller:

...
use App\Http\Requests\AreaRequest;
...

public function edit(Area $area,AreaRequest $request) //it works if I remove AreaRequest $request
{

    return view('backend.areas.edit', compact('area'));

}

public function create(AreaRequest $request)
{

    return view('backend.areas.create');

}

Request:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AreaRequest 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 [
        'name' => 'required|string'
    ];
}
}

Dev tools image:

enter image description here


Solution

  • You are using the form request in wrong method

    edit and create are for showing the form view

    the validation should goes to store or update

    public function store(AreaRequest $request)
    {
    
    }
    
    public function update(AreaRequest $request, Area $area)
    {
    
    }