Laravel:我添加所需的规则时,FormRequest验证失败

时间:2018-12-04 11:51:26

标签: javascript php laravel vue.js

我设置了一个FormRequest类,以验证前端(以FormData对象的形式)的输入,但是它对我的字段(标题和正文)的作用确实很奇怪。

尽管发送了FormData(我检查“网络”选项卡并执行$ request-> all()),但我仍需要标题和正文字段244验证错误,

我还注意到,删除必填规则后,即使我的两个输入字符均少于5个字符,验证仍成功通过(这不应该发生)。知道是什么原因造成的吗?

现在,如果需要的规则通过,我的输入将通过,如果我将其添加回数据库,则会将其添加到数据库,验证失败,并弹出必填消息。

我的FormRequest:

<?php

namespace App\Http\Requests\bulletins;

use Illuminate\Foundation\Http\FormRequest;

class CreateBulletin 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 [
            'image' => 'nullable|sometimes|image|mimes:jpeg,bmp,png,jpg,svg|max:2000',
            'title' => 'string|min:5|max:250',
            'body' => 'string|min:5|max:250',
        ];
    }
}

我的控制器方法:

public function store(CreateBulletin $request)
    {
        //dd($request->all());
        $bulletin = new Bulletin();
        $bulletin->title = $request->input('title');
        $bulletin->body = $request->input('body');
        $bulletin->image = '/img/others/default.jpg';

        if($request->hasFile('image')){
            $uploadFile = $request->file('image');
            $filename = str_random(6).'.'.$uploadFile->extension();
            $uploadFile->storeAs('uploads', $filename);
            $bulletin->image = '/uploads/'.$filename;
        }

        $bulletin->save();
        
        return response()->json([
            'bulletin' => $bulletin,
        ]);
    }

我发送的数据的形状:

检查在“网络”标签上发送的参数:

-----------------------------1607382142848
Content-Disposition: form-data; name="title"

title of bulletin
-----------------------------1607382142848
Content-Disposition: form-data; name="body"

content of bulletin
-----------------------------1607382142848--

OR

做完dd($ request-> all())

array:3 [
  "title" => "title of bulletin"
  "body" => "content of bulletin"
  "image" => UploadedFile {#971
    -test: false
    -originalName: "01-uxpin-modal-signup-form.jpg"
    -mimeType: "image/jpeg"
    -error: 0
    #hashName: null
    path: "C:\xampp\tmp"
    filename: "php7708.tmp"
    basename: "php7708.tmp"
    pathname: "C:\xampp\tmp\php7708.tmp"
    extension: "tmp"
    realPath: "C:\xampp\tmp\php7708.tmp"
    aTime: 2018-12-04 11:45:56
    mTime: 2018-12-04 11:45:56
    cTime: 2018-12-04 11:45:56
    inode: 0
    size: 48989
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "C:\xampp\tmp\php7708.tmp"
  }
]

因此您可以看到我的数据进入了服务器

2 个答案:

答案 0 :(得分:0)

尝试类似的方法,看看是否出现相同的错误:

   <?php

      namespace App\Http\Requests\bulletins;

      use App\Http\Requests\Request;

      class CreateBulletin extends Request
      {
        /**
        * 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 [
             'image' => 'nullable|sometimes|image|mimes:jpeg,bmp,png,jpg,svg|max:2000',
             'title' => 'string|min:5|max:250',
             'body' => 'string|min:5|max:250',
           ];
        }
   }

需要从类中删除FormRequest才能删除替换Request后遇到的错误。 希望这会有所帮助。

答案 1 :(得分:0)

在设置值之前,您必须使用:

$validated = $request->validated();

然后(例如,在表单请求中具有规则的标题字段):

$bulletin->title = $validated->input('title');

还要确保控制器中的导入:

use Illuminate\Http\Request; for the request; 

use App\Http\Requests\CreateBulletin;