Warm tip: This article is reproduced from stackoverflow.com, please click
frameworks laravel laravel-6 php validation

Laravel Custom Validation Rule with multiple parameters

发布于 2020-04-18 09:53:51

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.

Questioner
skns
Viewed
183
Foued MOUSSI 2020-02-04 23:47

You may use starts_with :

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