0
votes

I have a FormRequest class set up to validate input from my frontend (in the shape of the 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 SUCCESSFULLY 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 the 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

after 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

2
Is it showing same result if you use App\Http\Requests\Request in place of Illuminate\Foundation\Http\FormRequest in your custom request?kshitij
I did what you said here and got the following error: message: Class 'App\Http\Requests\bulletins\FormRequest' not foundgabogabans

2 Answers

0
votes

Try something like this and see if you get same error:

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

Need to remove FormRequest from class to remove that error you got after replacing Request. Hope this helps.

0
votes

Before set the values you have to test the request using:

$validated = $request->validated();

then(for example the title field which has a rule in the form request):

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

Make sure of the imports in the controller also:

use Illuminate\Http\Request; for the request; 

and

use App\Http\Requests\CreateBulletin;