Sam Teng Wong
Sam Teng Wong

Reputation: 2439

laravel request validation rules pass parameter

I'm going straight to the point here, I am wondering if it is possible to pass a parameter on a validation rule in Laravel.

Here's my code:

I need to pass the $product->id to the ProductUpdateRequest class.

I've read some articles and to no avail can't pass a parameter into it. my other solution was to not use the validation rule class and do the validation directly on the controller by using $request->validate[()]. Since I can access the $product->id on the controller I can easily do the validation. but out of curiosity is there a way for me to pass the $product->id on the validation class?

CONTROLLER

public function update(ProductUpdateRequest $request, Product $product)
    {
        $request['detail'] = $request->description;
        unset($request['description']);
        $product->update($request->all());

        return response([
            'data' => new ProductResource($product)
        ], Response::HTTP_CREATED);
    }

VALIDATION RULE

public function rules()
{
    return [
        'name' => 'required|max:255|unique:products,name'.$product->id,
        'description' => 'required',
        'price' => 'required|numeric|max:500',
        'stock' => 'required|max:6',
        'discount' => 'required:max:2'
    ];
}

Any suggestions/answers/help would be highly appreciated.

Upvotes: 12

Views: 23749

Answers (3)

chanafdo
chanafdo

Reputation: 5134

You can get the resolved binding from request

$product = $this->route('product');

Inside your rules method you can get the product instance with the above method.

public function rules()
{
    $product = $this->route('product');
    return [
        'name' => 'required|max:255|unique:products,name'.$product->id,
        'description' => 'required',
        'price' => 'required|numeric|max:500',
        'stock' => 'required|max:6',
        'discount' => 'required:max:2'
    ];
}

It works when you make a function with this Product $product (when you used the Resource route in most cases) public function update(ProductUpdateRequest $request, Product $product) { // code goes here }

but if you make it like the below it won't work () public function update(ProductUpdateRequest $request, $id) { // code goes here }

Upvotes: 19

Hesam Moosapour
Hesam Moosapour

Reputation: 592

For custom request in validation rule you can put in your View :

<input type="hidden" value="product_id">

In Validation Request :

public function rules()
{
    $product_id = $this->request->get('product_id');

    return [
      //
    ];
}

Upvotes: 1

bmatovu
bmatovu

Reputation: 4074

This is how I would validate unique product name on update. I pass the product ID as a route parameter, the use the unique validation rule to validate that it the product name does't exist in the Database except for this product (id).

class ProductController extends Controller {

    public function update(Request $request, $id) {
        $this->validate($request, [
            'name' => 'required|max:255|unique:products,name'.$id,
        ]);

        // ...
    }

}

Upvotes: 2

Related Questions