R
R
Rodion Yurchenko2019-11-12 14:30:52
Laravel
Rodion Yurchenko, 2019-11-12 14:30:52

What is the correct way to use custom validation rule in formRequest for more than one parameter?

Good afternoon
I'm asking this question not for the first time - because usually it's not always possible to convey what exactly "doesn't work", so sorry for the extra detail I'm
using Laravel v5.8
There is a form that comes with 3 parameters A,B,C
For standard validation, usually I'm used to using FormRequest, and in this case it will look something like this:

public function rules()
    {
        return [
            'A'=>['required','min:2'],
            'B'=>['required','min:5'],
            'C'=>['required','email'],
        ];
    } // func

Further, if I need to check some field for more complex logic that does not fit into the standard rules, I create a custom Rule and stuff this very logic there - from now on, the validation method in the FormRequest looks something like this:
public function rules()
    {
        return [
            'A'=>['required','min:2', new CustomValidationRule],
            'B'=>['required','min:5'],
            'C'=>['required','email'],
        ];
    } // func

As we know, in CustomValidationRule there is a method passes to which $attribute, $value are passed - that is, everything that is needed to check field A, but other parameters are not passed there.
My situation is that I need to check not just each field separately for some kind of logic , but all the fields at once, something like if A=5 then B should be greater than 22 and C should be soap from gmail and not from mail.ru
To begin with, I thought about the withValidator method that can be described in FormRequest and add checks there
But if I will need this custom check in another FormRequest, so it turns out I will have to duplicate the code
. Actually, this is the question - how to make a custom rule in which you can pass several parameters from the request at once, and not just one key-value pair?
How and from where is it correct to call this rule so that it will work immediately after validation by static rules?
Maybe it's not the same toolkit at all?
I can create a separate class with separate methods for checking - inject it into a FormRequest, but won't it work so that I create a bike when everything is already there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NubasLol, 2019-11-12
@aassdds

CustomValidationRule not only has a passes method, but it also has a __construct method into which you can pass anything.
For example $this->all() in the request class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question