FormRequest validation failing when I add required rule despite data hitting the server


#1

Hi everybody, I have a FormRequest class set up to validate input from my frontend (in the shape of FormData object) however it’s acting really weird with my fields (title and body).

Despite FormData being sent (I check network tab and do $request->all()) I’m getting the title and body fields are required 244 validation error,

I also noticed that after removing the required rule the validation PASSES SUCCESFULLY even if my both my inputs are less than 5 characters (this shouldn’t happen). Any idea what could be causing this?

So right now if required rule my input passes and is added to database, if I add it back validation fails and field required message pops up.

My 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',
        ];
    }
}

My controller method:

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,
        ]);
    }

The shape of my data being sent:

Checking parameters sent at network tab:

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

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

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

OR

doing 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"
  }
]

So as you can see my data hits the server