0
votes

I want to add a custom validation rules with parameter and in the Laravel documentation https://laravel.com/docs/5.8/validation#custom-validation-rules there is no validation parameters option. I want a validation rule like required_if.

2

2 Answers

2
votes

You can pass it in the constructor

new CustomRule($parameter);
class CustomRule implements Rule
{
    public function __construct($parameter)
    {
        $this->parameter = $parameter;
    }

...
0
votes

You can use sometimes method on validator instance. Use a required rules and add your condition in the function as below:

$v->sometimes('reason', 'required|max:500', function ($input) {
    return $input->games >= 100;
});

The example above will check if games are more than or equal to 100, then user will required to have a reason, otherwise it will trigger an error.

The example is from Laravel documentation that you can find here here