如果值存在于另一个字段数组中,则 Laravel 验证规则

Laravel Validation Rules If Value Exists in Another Field Array(如果值存在于另一个字段数组中,则 Laravel 验证规则)
本文介绍了如果值存在于另一个字段数组中,则 Laravel 验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 Laravel 5.4 中工作,我需要一个稍微具体的验证规则,但我认为这应该很容易实现,而无需扩展类.只是不知道如何使这项工作..

如果 program 数组包含 'Music',我想做的是将 'music_instrument' 表单域设为必填.>

我发现了这个帖子 如何设置 require 如果在验证 laravel 的另一个多选字段中选择了值? 但这不是一个解决方案(因为它首先从未得到解决)以及它没有解决的原因't work 是因为提交的数组索引不是常量(在索引提交结果时不考虑未选中的复选框...)

我的情况是这样的:

<字段集><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">提交</button></fieldset></表单>

如果我从复选框列表中选择一些选项,我可能会在我的 $request 值中得到这个结果

[程序] =>大批([0] =>人类学[1] =>生物学[2] =>音乐[3] =>哲学)[音乐乐器] =>'吉他'

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

 $validator = Validator::make($request->all(),['程序' =>'必需的','music_instrument' =>'required_if:program,in:Music']);

我希望这也能奏效,但没有运气:

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

想法?建议?

谢谢!

解决方案

没试过,但在一般的数组字段中你通常这样写:program.*,所以可能是这样的将工作:

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

如果它不起作用,显然你也可以用另一种方式来做,例如这样:

$rules = ['program' =>'必需的'];if (in_array('Music', $request->input('program', []))) {$rules['music_instrument'] = 'required';}$validator = Validator::make($request->all(), $rules);

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!

解决方案

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 验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)