Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Validation Rules If Value Exists in Another Field Array

I am working in Laravel 5.4 and I have a slightly specific validation rules need but I think this should be easily doable without having to extend the class. Just not sure how to make this work..

What I would like to do is to make the 'music_instrument' form field mandatory if program array contains 'Music'.

I found this thread How to set require if value is chosen in another multiple choice field in validation of laravel? but it is not a solution (because it never got resolved in the first place) and the reason it doesn't work is because the submitted array indexes aren't constant (not selected check boxes aren't considered in indexing the submission result...)

My case looks like this:

<form action="" method="post">
    <fieldset>

        <input name="program[]" value="Anthropology" type="checkbox">Anthropology
        <input name="program[]" value="Biology"      type="checkbox">Biology
        <input name="program[]" value="Chemistry"    type="checkbox">Chemistry
        <input name="program[]" value="Music"        type="checkbox">Music
        <input name="program[]" value="Philosophy"   type="checkbox">Philosophy
        <input name="program[]" value="Zombies"      type="checkbox">Zombies

        <input name="music_instrument" type="text" value"">

        <button type="submit">Submit</button>

    </fieldset>
</form>

If I select some of the options from the list of check boxes I can potentially have this result in my $request values

[program] => Array
    (
        [0] => Anthropology
        [1] => Biology
        [2] => Music
        [3] => Philosophy
    )

[music_instrument] => 'Guitar'

Looking at validation rules here: https://laravel.com/docs/5.4/validation#available-validation-rules I think something like his should work but i am literally getting nothing:

  $validator = Validator::make($request->all(),[
        'program'           => 'required',
        'music_instrument'  => 'required_if:program,in:Music'
  ]);

I was hoping this would work too but no luck:

'music_instrument'  => 'required_if:program,in_array:Music',

Thoughts? Suggestions?

Thank you!

like image 233
GRowing Avatar asked Mar 31 '17 18:03

GRowing


2 Answers

Haven't tried that, but in general array fields you usually write like this: program.*, so maybe something like this will work:

  $validator = Validator::make($request->all(),[
        'program'           => 'required',
        'music_instrument'  => 'required_if:program.*,in:Music'
  ]);

If it won't work, obviously you can do it also in the other way for example like this:

$rules = ['program' => 'required'];

if (in_array('Music', $request->input('program', []))) {
    $rules['music_instrument'] = 'required';
}

$validator = Validator::make($request->all(), $rules);
like image 111
Marcin Nabiałek Avatar answered Oct 17 '22 07:10

Marcin Nabiałek


I know this post is older but if someone came across this issue again.

$validator = Validator::make($request->all(),[
    'program' => 'required',
    'music_instrument'  => 'required_if:program,Music,other values'
]);
like image 7
Noman Sheikh Avatar answered Oct 17 '22 07:10

Noman Sheikh