Laravel验证规则,如果值存在于另一个字段数组中 [英] Laravel Validation Rules If Value Exists in Another Field Array

查看:66
本文介绍了Laravel验证规则,如果值存在于另一个字段数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5.4中工作,我有一个稍微特定的验证规则,但我认为这很容易做到,而不必扩展类.只是不确定如何使这项工作..

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..

我想做的是,如果program数组包含'Music',则必须使'music_instrument'表单字段为必填项.

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...)

我的情况如下:

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

如果我从复选框列表中选择某些选项,则可能会在$request值中得到该结果

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'

在此处查看验证规则: https://laravel.com/docs/5.4/validation#available-validation-rules 我认为类似他的东西应该可以工作,但是我什么也没得到:

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?

谢谢!

推荐答案

还没有尝试过,但是在一般的数组字段中,您通常这样写:program.*,所以也许这样的事情会起作用:

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);

这篇关于Laravel验证规则,如果值存在于另一个字段数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆