Lazar Pavlovic
Lazar Pavlovic

Reputation: 53

Laravel 8 Form Request Validation Redirect to Index page instead same page and show error

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 class="form-group row">
      <div class="col">
         <label class="col-form-label">Name</label>
         <input id="name" type="text" class="form-control" name="name" value="{{ $config->name }}" required>
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">Address</label>
         <input id="address" type="text" class="form-control" name="address" value="{{ $config->address }}">
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">Phone</label>
         <input id="phone" type="tel" class="form-control" name="phone" value="{{ $config->phone }}" required>
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">E-mail</label>
         <input id="email" type="email" class="form-control" name="email" value="{{ $config->email }}" required>
      </div>
   </div>
   <div class="form-group row mt-4 mb-0">
      <div class="col-md-12">
         <button type="submit" class="btn btn-primary button-full-width">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?

Upvotes: 1

Views: 3902

Answers (2)

zehyr
zehyr

Reputation: 41

Do you have error parts in your blade?

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @if ($message = Session::get('unique'))
            asdsad
            @endif
        @endforeach
    </ul>
</div>
@endif

Upvotes: 0

mare96
mare96

Reputation: 3859

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.

Upvotes: 3

Related Questions