13

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.

2
  • some validation rules like unique, and exists have an option param. Is that what you're referring to? Commented Apr 10, 2018 at 5:01
  • hi @bmatovu here's should be my validation rule return[ 'name' => 'required|max:255|unique:products,name'.$product->id] Commented Apr 10, 2018 at 5:05

3 Answers 3

20

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 }

Sign up to request clarification or add additional context in comments.

7 Comments

Do you get any errors. 'product' should match the route parameter name.
Hi sir Error: Undefined variable: request
Sorry. My mistake. Change $request to $this
This does work! You are the man! :-) (at least in Laravel 5.7)
|
2

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,
        ]);

        // ...
    }

}

2 Comments

Hi sir.. this is my other solution.... but u think it is possible to do it on my Validation Class?
Ohhhh... I was wondering too, you have the solution. The question wasn't as clear. Sorry my validation class, why not just rule a new validation rule, then use your custom validation logic?
1

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 [
      //
    ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.