Atef95
1 week ago

Form Request issue

Posted 1 week ago by Atef95

Hey guys

I'm using Laravel & vue

I'm trying to validate an upcoming request using the form request..

I'm passing data from the front and it's coming correctly

the issue validation is always throwing 422 errors

that's an example of the upcoming request

array:18 [
  "active" => 1
  "title" => "dsdsd"
  "categories" => array:1 [
    0 => array:6 [
      "id" => 1
      "name" => "Psychologie"
      "deleted_at" => null
      "created_at" => null
      "updated_at" => null
      "disabled" => true
    ]
  ]
  "languages" => array:1 [
    0 => array:6 [
      "id" => 1
      "name" => "Anglais"
      "deleted_at" => null
      "created_at" => null
      "updated_at" => null
      "disabled" => true
    ]
  ]
  "authors" => []
  "published_year" => "5454"
  "editor" => "54s5d4sd"
  "count_pages" => "54545"
  "isbn" => "5sd4s54d"
  "description" => "sdsdsd"
  "height" => null
  "width" => null
  "thickness" => null
  "weight" => null
  "paper_price" => null
  "digital_price" => null
  "photo" => "/img/placeholder.jpg"
 ]

this is StoreBook FormRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBook 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()
    {

        $this->request->get('book');
        return [
            'title' => 'required|max:200',
            'published_year' => 'required|numeric|digits:4||digits:' . (date('Y') + 1),
            'editor' => 'required|max:90',
            'count_pages' => 'required|integer|min:1',
            'isbn' => 'required|max:90',
            'description' => 'required|max:200',
            'active' => 'required|in:0,1',
            'height' => 'nullable|between:0,99.9999',
            'width' => 'nullable|between:0,99.9999',
            'thickness' => 'nullable||between:0,99.9999',
            'weight' => 'nullable|between:0,99.9999',
            'paper_price' => 'nullable|between:0,99.9999',
            'digital_price' => 'nullable|between:0,99.9999',
            'photo' => 'nullable',
            'digital_link' => 'nullable',
        ];

    }

    public function messages()
    {
        return [
            'title.required' => 'Le titre est requis',
            'title.max' => 'Le titre est non valide',
            'published_year.required' => 'L\'Année de publication est requise',
            'published_year.numeric' => 'L\'Année de publication est invalide',
            'published_year.digits' => 'L\'Année de publication est invalide',
            'published_year.digits' => 'L\'Année de publication est invalide',
            'editor.required' => 'L\'editeur est requis',
            'editor.max' => 'L\'editeur est invalide',
            'count_pages.required' => 'Nombre des pages est requis',
            'count_pages.integer' => 'Nombre des pages est invalide',
            'count_pages.min' => 'Nombre des pages est invalide',
            'isbn.required' => 'ISBN est requis',
            'isbn.max' => 'ISBN est invalide',
            'description.required' => 'La description est requise',
            'description.max' => 'La description est invalide',
            'active.required' => 'L\'etat est requis',
            'active.in' => 'L\'etat est invalide',
            'height.required' => 'L\'hauteur est requis',
            'height.between' => 'L\'hauteur est invalide',
            'width.required' => 'Le largeur est requis',
            'width.between' => 'Le largeur est invalide',
            'thickness.required' => 'L\'epaisseur est requis',
            'thickness.between' => 'L\'epaisseur est invalide',
            'weight.required' => 'Le poids est requis',
            'weight.between' => 'Le poids est invalide',
            'paper_price.between' => 'Le prix version papier est invalide',
            'digital_price.between' => 'Le prix version numérique est invalide',
        ];
    }
}

My controller

 public function handleSaveBook(StoreBook $request)
    {
      
        $saveBook = $this->books->store();
        return response()->json(['status' => 200]);
    }

what's causing that ?

Please sign in or create an account to participate in this conversation.