0

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 Answers 2

2

You can pass it in the constructor

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

...
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.