Home > Back-end >  Laravel 8 Form Request Validation Redirect to Index page instead same page and show error
Laravel 8 Form Request Validation Redirect to Index page instead same page and show error

Time:12-14

On localhost all is good, but when I deploy the application to the server not working. If form request validation fails instead of bringing me back to the same page and showing an error, it redirects me to the index page.

config.blade.php

<form method="POST" action="{{ route('config.update', $config->id) }}">
   @csrf
   @method('PUT')
   <div >
      <div >
         <label >Name</label>
         <input id="name" type="text"  name="name" value="{{ $config->name }}" required>
      </div>
   </div>
   <div >
      <div >
         <label >Address</label>
         <input id="address" type="text"  name="address" value="{{ $config->address }}">
      </div>
   </div>
   <div >
      <div >
         <label >Phone</label>
         <input id="phone" type="tel"  name="phone" value="{{ $config->phone }}" required>
      </div>
   </div>
   <div >
      <div >
         <label >E-mail</label>
         <input id="email" type="email"  name="email" value="{{ $config->email }}" required>
      </div>
   </div>
   <div >
      <div >
         <button type="submit" >Save changes</button>
      </div>
   </div>
</form>

web.php

Route::resource('/admin/config', 'Admin\ConfigController');

ConfigController

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\ConfigServices;
use App\Http\Requests\ConfigRequest;
use App\Models\Config;

class ConfigController extends Controller
{
    protected $configServices;

    public function __construct(ConfigServices $configServices) {
        $this->middleware('auth');
        $this->configServices = $configServices;
    }

    ...

    public function update(ConfigRequest $request, $id)
    {
        $config = $this->configServices->updateConfigById($request, $id);
        return redirect()->back();
    }

    ...

}

ConfigRequest - here is the problem

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ConfigRequest 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|max:255',
            'address' => 'nullable|string|max:255',
            'phone' => 'required|regex:/^([0-9\s\-\ \(\)]*)$/|min:9|max:15',
            'email' => 'required|email:rfc',
        ];
    }
}

Form Request return to index page instead same page. On localhost working everything, but when I deploy the app to server a problem arises. When data on form request validated correct return me back on the same page and show success, but when form request failing redirect mine for some reason to the index page.

A problem arises in Laravel 8, this code worked well in previous Laravel versions.

Can someone help me, please?

CodePudding user response:

In your custom request you need:

/**
 * The URI that users should be redirected to if validation fails.
 *
 * @var string
 */
protected $redirect = '/dashboard';

or

/**
 * The route that users should be redirected to if validation fails.
 *
 * @var string
 */
protected $redirectRoute = 'dashboard';

You can find more in the docs.

In the docs for older versions of Laravel these properties don't exist.

  • Related