如果表单验证失败,则使用自定义请求类(laravel 5.8)使用输入重定向回

时间:2019-09-22 04:33:27

标签: php laravel forms laravel-5.8

问题是验证失败时我想返回输入。在这里,我使用自定义请求类来验证用户输入。

  

现在我的问题是我将这段代码放在 Controller Request类中的什么位置。
  还是有另一种方法?

这是我要使用的代码: return redirect()->back()->withInput();

这是控制者:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\counter\CreateCounterRequest;
use App\Models\Admin\Counter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CounterController extends Controller
{
    public function store(CreateCounterRequest $request)
    {
        Counter::create([
            'title' => $request->title,
            'ion_icon' => $request->ion_icon,
            'counter_value' => $request->counter_value,
        ]);
        session()->flash('success', 'Counters created successfully.');
        return redirect(route('counter.index'));
    }
}

这是请求类别:

<?php

namespace App\Http\Requests\Admin\counter;

use Illuminate\Foundation\Http\FormRequest;

class CreateCounterRequest extends FormRequest
{
    public function rules()
    {
        return [
            'ion_icon' => 'required',
            'title' => 'required',
            'counter_value' => 'required|numeric',
        ];
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用 FormRequest 类中的方法,即 failedValidation()方法。

,示例代码如下:

protected function failedValidation(Validator $validator)
{
    return back()->withErrors($validator)->withInput();
}

您必须将 Illuminate \ Contracts \ Validation \ Validator 类中的参数添加到方法 failedValidation 中,并添加 Illuminate \ Http \ RedirectResponse 类获取重定向方法。

添加 Illuminate \ Validation \ ValidationException 类,如果不能,则无法读取您的方法。

答案 1 :(得分:0)

请检查此

use Illuminate\Support\Facades\Validator;
protected function validator(array $data)
    {
        return Validator::make($data, [
            'ion_icon' => 'required',
            'title' => 'required',
            'counter_value' => 'required',
        ]);
    }
public function store(Request $request)
    {

        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            return redirect()->route('your_route')->withInput(['title' => $request->title, 'icon_icon' => $request->icon_icon,'counter_value' => $request->couter_value,'phone_number'=>$request->phone_number])->withErrors($validator, 'your_desire_name');
        }
        Counter::create([
            'title' => $request->title,
            'ion_icon' => $request->ion_icon,
            'counter_value' => $request->counter_value,
        ]);
        session()->flash('success', 'Counters created successfully.');
        return redirect(route('counter.index'));
    }

想在刀片文件中显示此错误消息

<div >

                                <input type="text" name="title" class="form-control"
                                       value="{{old('title')}}" required>


                            </div>
                            @if ($errors->your_desire_name->has('title'))


                                <span class="messages"><p
                                        class="text-danger error">{{ $errors->your_desire_name->first('title') }}</p></span>



                            @endif