【问题标题】:Laravel form request validation return errorLaravel 表单请求验证返回错误
【发布时间】:2021-04-10 22:54:41
【问题描述】:

我正在使用 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') ;
    }

【问题讨论】:

  • 422 是验证错误响应...您如何发送此请求?
  • @lagbox 直通刀片,通常通过路线我已经更新我的帖子并添加我的刀片
  • 它返回 422 而没有进行重定向的事实意味着它认为这是某种类型的 ajax 请求或期待 json 返回的东西
  • @lagbox 但我不是,如果它很重要,我正在使用 nwidart laravel 模块
  • 另外,如果您使用的是 Laravel 5 的某个版本,为什么还要阅读 Laravel 8 文档?

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


【解决方案1】:

你正在使用什么版本的 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 中。

我希望这会有所帮助。

【讨论】:

  • 我的 laravel 5.6,我试过了,结果一样
  • 试试php artisan view:clearcomposer dump-autoload。然后再试一次。你至少应该得到不同的回应。
  • 同样的事情,但我有点解决了。如果我使用 .Validator::make($request,$rules) 它工作。现在我如何在SpecializedRequest上使用Validator::make
猜你喜欢
  • 2020-01-17
  • 2015-09-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2016-08-30
  • 2017-11-07
  • 2017-11-07
  • 2017-07-16
相关资源
最近更新 更多