P
P
Pichenka942019-11-06 06:47:17
Laravel
Pichenka94, 2019-11-06 06:47:17

How to pass a variable to Validator?

Hello, the question is in the title.
Now says that the variable is undefined, the code is as follows

...
protected $phone_number;

public function __construct()
{
   $this->middleware('guest');
   $this->phone_number = request()->input('phone');
}

protected function validator(array $data)
{
    return Validator::make($data, [
            ...
            'verification_code' =>  Rule::exists('phone_numbers', 'verification_code')->where(function ($query) use ($phone_number) {
                $query->where('phone_number', $phone_number);
            }),
    ]);
}
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2019-11-06
@Pichenka94

Everything speaks correctly, but you are accessing it incorrectly, you set it as $this->phone_number, and you call it as $phone_number.
Therefore, define it before calling the validator Or call it from the object context$phone_number = $this->phone_number;

'verification_code' =>  Rule::exists('phone_numbers', 'verification_code')->where(function ($query) {
                $query->where('phone_number', $this->phone_number);
            }),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question