Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kaustubh's avatar

how to pass variable in validation rule

Hi guys,

I want to validate text field if Commission amount is less than 4.5% on Salary

<input type="text" id="salary" name="salary">
<input type="text" id="commission" name="commission" placeholder="Amt should be >= 4.5% on salary">

public function hrCJPsave(Request $req)
{  
     $salary => REQUEST('salary');
     $commission = $salary*4.5/100;

    $input = $req->all();
    $validator = Validator::make($input, [
        'salary'            =>  'required|max:2',
        'commission'        =>  'required|min:$commission'            
    ]);
}

How can i pass variable in validator

0 likes
1 reply
Dunsti's avatar
Dunsti
Best Answer
Level 6

PHP-Variables within single quotes are not parsed.

either use:

"required|min:$comission"

or

'required|min:' . $comission ( I would prefer this one)

5 likes

Please or to participate in this conversation.