K
K
Konstantin Malyarov2021-03-16 18:35:24
Laravel
Konstantin Malyarov, 2021-03-16 18:35:24

How to change route on wrong laravel validation in request?

There is a request in which the form is validated, how can I route with an anchor if an error occurs?

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CallbackRequest 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 [
            'telephone' => 'required|max:12',
            'name' => 'required|max:20'
        ];
    }

    public function messages()
    {
        return [
            'telephone.required' => 'Поле Телефон - обязательно.',
            'name.required' => 'Поле Имя - обязательно.',
            'telephone.max' => 'Телефон введен не верно.',
            'name.max' => 'Введите короткую форму имени.'
        ];
    }
}

If the form is filled out correctly, then I simply redirect with the anchor:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\CallbackRequest;
use App\Models\Callback;

class CallbackController extends Controller
{

    public function submit(CallbackRequest $callback_request)
    {
        $callback = new Callback();
        $callback->name=$callback_request->input('name');
        $callback->telephone=$callback_request->input('telephone');
        $callback->save();

        return redirect()->route('welcome', '#callback');
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question