davy_yg
1 week ago

Validation using Request

Posted 1 week ago by davy_yg

Hello,

I am trying to use request for validation:

ref: https://laravel.com/docs/5.7/validation#form-request-validation

Here is my existing validation:

modules/ProductPriceList/Http/Requests/Store.php

<?php

namespace Modules\ProductPriceList\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Store extends FormRequest
{
    /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */

public function rules()
    {
        return [
            'sku' => 'required|unique:product_price_lists,sku,NULL,id,deleted_at,NULL,digits:13',
            'sku_external' => 'nullable|unique:product_price_lists,sku_external,NULL,id,deleted_at,NULL',
            'product_catalog_id' => 'required',
            'variant' => 'required',
            'bin_code' => 'required',
            'unit' => 'nullable|string',
            'price' => 'required|integer',
            'price_before_discount' => 'nullable|integer|gte:price'
    // 'min_value' => 'required|numeric|min:0',
    // 'max_value' => 'required|numeric|min:0',
        ];
    }

modules/ProductPriceList/Http/Requests/Update.php

<?php

namespace Modules\ProductPriceList\Http\Requests;

class Update extends Store
{
    /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */

    public function rules()
    {
    $skuExternal = 'nullable|unique:product_price_lists,sku_external,';
    return [
        'sku' => 'required|unique:product_price_lists,sku,' . $this->productPriceList->id . ',id,deleted_at,NULL',
        'sku_external' => $skuExternal . $this->productPriceList->id . ',id,deleted_at,NULL',
        'product_catalog_id' => 'required',
        'variant' => 'required',
        'bin_code' => 'required',
        'unit' => 'nullable|string',
        'price' => 'required|integer',
        'price_before_discount' => 'nullable|integer|gte:price'
     // 'min_value' => 'required|numeric|min:0', 
     // 'max_value' => 'required|numeric|min:0',
        ];
    }

First, I wonder why it uses extends Store instead of extends FormRequest?

Next, under which directory would you write:

php artisan make:request Store

So that the request will appears on the correct module folder?

Next, why would people use php artisan make:request Store to validate when you could do it in the controller?

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