Ad

Laravel Custom Validation Rule With Multiple Parameters

I have to validate a field with the input given in another field in custom validation rule.

I have a fields in1 and in2, both are strings and in1 should start with in2.

From the documentation, I can create something like this

public function passes($attribute, $value)
{
    return true;
}

Where attribute have field name(ex:in1) and value have value of that field. How can I pass in2 to this.

Ad

Answer

You may use starts_with :

$validator = Validator::make($request->all(), [
   'in1' => '...|...', 
   'in2' => 'required_if:in1|starts_with:' . $request->input('in1')
]);
Ad
source: stackoverflow.com
Ad