31

I have defined an array

$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');

Now, I want to validate before saving data to database that, if a given input matches one of the value of the above array. For this I'm doing

$this->validate($request, [

        'field' => 'required|in_array:$this->allslots',
    ]);

But, this returns validation error for every input. So, how can I do this?

0

3 Answers 3

69

Try this:

'field' => 'required|in:' . implode(',', $this->allslots),
Sign up to request clarification or add additional context in comments.

1 Comment

What if one of the array items contains a comma? Then you're really screwed. :)
44

This is a bit more semantic than the accepted answer:

use Illuminate\Validation\Rule;

$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');

$request->validate([
    'field' => [
        'required',
        Rule::in($this->allslots)
    ]
]);

Comments

3

I found a better solution. The validate in_array expects the array to be one of the parameters in the request. The accepted answer will not work if you have commas in the array. To use the in_array without having to create a new rule you can simply do:

$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');

$request['allslots'] = $this->allslots;

validate($request, [

        'field' => 'required|in_array:allslots.*',
    ]);

Make sure you include the .* at the end

2 Comments

It is not simpler (about the same) and alters the request data.
Maybe "simpler" is not the best word, but it does resolve the coma issue. The accepted answer has a big weakness, it assumes there is no comma, so it won't work in some situations. The solution I propose does not require you to create any additional rule, plus it works in every case. And no, you are not altering the requested data, you are "adding" a field, not much different than adding a @method('PATCH'). However point taken and I will remove the word "simpler".

Your Answer

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