Laravel 表单请求验证返回错误

时间:2021-01-05 07:26:58

标签: laravel laravel-5 laravel-validation laravel-formrequest

我正在使用 Laravel 表单请求验证来验证从视图到控制器的请求。我正在使用 php artisan make:request SpecializedRequest。但是当验证失败时,它不会返回并给出错误 422。我查看了 laravel documentation,但我真的不明白。我如何确定如果验证失败,它会返回上一页并显示错误消息 我的表单请求验证

<?php

namespace Modules\Specialized\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Modules\Specialized\Entities\Specialized;

class SpecializedRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => 'Nama Specialized cannot be empty',
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

我的控制器

/**
     * Store a newly created resource in storage.
     * @param  SpecializedRequest $request
     * @return Response
     */
    public function store(SpecializedRequest $request)
    {
        $specialized = Specialized::create($request->validated());
        return ($specialized) ? back()->withSuccess('Data has been added') : back()->withError('Something wrong') ;
    }

我的刀

<form action="{{ route('specialized.store') }}" method="{{ $method }}" class="form-horizontal">
    @csrf   
    <fieldset class="content-group">
        <div class="form-group">
            <label for="name" class="control-label col-lg-2">Name</label>
            <div class="col-lg-10">
                <input type="text" name="name" class="form-control" value="{{ old('name',isset($specialized->name) ? $specialized->name : '') }}">
                <span style="color:red;"> {{$errors->first('name')}} </span>
            </div>
        </div>

    </fieldset>


    <div class="text-right">
        <button type="submit" class="btn btn-primary">Submit <i class="icon-arrow-right14 position-right"></i></button>
    </div>
</form>

尝试了同样的结果

public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required',
        ]);
        $specialized = Specialized::create($data->validated());
        return ($specialized) ? back()->withSuccess('Data has been added') : back()->withError('Something wrong') ;
    }

1 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的 Laravel?

我认为你不需要:

$specialized = Specialized::create($request->validated());

试试:

$specialized = Specialized::create($request->all());
return redirect()->back() ;

然后处理刀片中的错误显示,例如:

@if($errors->any()){{ implode('', $errors->all('<div>:message</div>')) }}@endif

另外,我相信您需要:

    /**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

在您的 SpecializedRequest 中。

我希望这会有所帮助。