I'm trying to create a custom validation rule that accept a parameter, but this parameter is the name of another field in the request, like for the required_with
rule.
I easily can handle given params in my rule, but i'm struggling to find out how to retrieve the other field value.
Currently i'm creating my rule class as
class MyClassRule
{
public function validate($attribute, $value, $parameters, $validator) : bool
{
// do some stuff here to return true/false
}
}
and registering it in my service provider with
Validator::extend('my_rule', 'path\to\MyClassRule@validate');
so i can use it in my request as
public function rules()
{
return [
'field' => ['my_rule'],
];
}
What i would like to be able to do is
public function rules()
{
return [
'other_field' => [...],
'field' => ['my_rule:other_rule'],
];
}
and use the other_field
value in my rule class, but validate()
's $parameters
value is just ['other_field']
. i.e. an array containing the other field name, not its value.
How can i do this?
$validator
param, none of the examples/tutorials i found used it. – Burgee