skip to Main Content

I have not so much practical experience with Laravel yet and I wondered what is the best way to deal with similar validation logic and where to put it.

Let’s say I have an API resource Controller for Products with a store and an update method like so:

public function store(Request $request)
{
    $request->validate([
        'name' => 'required|string|max:100',
        'description' => 'nullable|string|max:1000',
        'price' =>'required|decimal:0,2|lt:1000'
    ]);

    return Product::create($request->all());
}

public function update(Request $request, Product $product)
{
    $request->validate([
        'name' => 'string|max:100',
        'description' => 'nullable|string|max:1000',
        'price' =>'decimal:0,2|lt:1000'
    ]);

    return Product::update($request->all());
}

The only difference between the validation in store and update is that store adds the ‘required’ rule for ‘name’ and ‘price’. My question is, if I can encapsulate both validations in one Form Request, or how can I avoid code duplication without adding unnecessary code?

With my understanding of Form Requests I would probably create two Form Request classes, StoreProductRequest and UpdateProductRequest, and maybe another helper class that defines the core validation rules. Then each Form request could call for example ProductHelper::getBaseValidationRules() and merge that with their extra requirements. Somehow I find that a bit overkill.

2

Answers


  1. Chosen as BEST ANSWER

    Okay based on the suggestions, I came up with the following solution:

    I created a Form Request named ProductRequest and implemented the rules method as follows:

    public function rules()
    {
        $rules = [
            'name' => ['string', 'max:100'],
            'description' => ['nullable', 'string', 'max:1000'],
            'price' => ['decimal:0,2', 'lt:1000'],
        ];
    
        // If the user wants to create a new Instance some fields are mandatory.
        if ($this->method() === 'POST') {
            $rules['name'][] = 'required';
            $rules['price'][] = 'required';
        }
    
        return $rules;
    }
    

    This is fine for me. Although in a bigger project I probably would create two Form Requests, StoreProductRequest and UpdateProductRequest. They would share and update a base set of rules as I described in the question.


  2. you can create a request for your validations and use them in your controllers

    for example

    php artisan make:request YOUR_REQUEST_NAME
    

    then inside your request you can add your validations like this

        public function rules()
        {
            return [
                'name' => 'required|string|max:100',
                'description' => 'nullable|string|max:1000',
                'price' => 'required|decimal:0,2|lt:1000'
            ];
        }
    
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    

    and in your method you can call it like this

    public function update(YOUR_REQUEST_NAME $request, Product $product)
    {
        return Product::update($request->all());
    }
    

    for more information you can read this
    https://laravel.com/docs/5.0/validation#form-request-validation
    in case you want condition in the rules please check this video
    https://www.youtube.com/watch?v=epMaClBOlw0&ab_channel=CodeWithDary

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search