如果值存在于另一个字段数组中,则 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 验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)