久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如果值存在于另一個字段數(shù)組中,則 Laravel 驗證

Laravel Validation Rules If Value Exists in Another Field Array(如果值存在于另一個字段數(shù)組中,則 Laravel 驗證規(guī)則)
本文介紹了如果值存在于另一個字段數(shù)組中,則 Laravel 驗證規(guī)則的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 Laravel 5.4 中工作,我需要一個稍微具體的驗證規(guī)則,但我認為這應(yīng)該很容易實現(xiàn),而無需擴展類.只是不知道如何使這項工作..

如果 program 數(shù)組包含 'Music',我想做的是將 'music_instrument' 表單域設(shè)為必填.>

我發(fā)現(xiàn)了這個帖子 如何設(shè)置 require 如果在驗證 laravel 的另一個多選字段中選擇了值? 但這不是一個解決方案(因為它首先從未得到解決)以及它沒有解決的原因't work 是因為提交的數(shù)組索引不是常量(在索引提交結(jié)果時不考慮未選中的復(fù)選框...)

我的情況是這樣的:

<字段集><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></表單>

如果我從復(fù)選框列表中選擇一些選項,我可能會在我的 $request 值中得到這個結(jié)果

[程序] =>大批([0] =>人類學[1] =>生物學[2] =>音樂[3] =>哲學)[音樂樂器] =>'吉他'

在此處查看驗證規(guī)則:https://laravel.com/docs/5.4/validation#available-validation-rules 我認為像他這樣的東西應(yīng)該可以工作,但我實際上什么也沒得到:

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

我希望這也能奏效,但沒有運氣:

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

想法?建議?

謝謝!

解決方案

沒試過,但在一般的數(shù)組字段中你通常這樣寫: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);

這篇關(guān)于如果值存在于另一個字段數(shù)組中,則 Laravel 驗證規(guī)則的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Magento products by categories(按類別劃分的 Magento 產(chǎn)品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 999国产视频| 久久久成人网 | 可以免费观看的av片 | 色综网| 欧美日韩国产一区 | 一区二区三区在线免费观看视频 | 久久久久久亚洲国产精品 | 国产一区二区三区四区三区四 | 91色网站| 国产高清在线精品一区二区三区 | 欧美日韩精品中文字幕 | 一区二区三区免费观看 | 欧美精品一区二区三区在线播放 | 色婷婷国产精品 | 欧美精品一区二区在线观看 | 亚洲第一网站 | 91xxx在线观看 | 精品久久久久久久久久久久久久 | 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 欧美在线视频免费 | 欧美成人免费 | а天堂中文最新一区二区三区 | 在线免费看黄 | 99精品网| 国产成人麻豆免费观看 | 久久久久久久久久久福利观看 | 中文字幕一区在线观看视频 | 日韩在线精品视频 | a免费视频 | 天堂一区在线观看 | 成人黄色av网站 | 在线视频a | 久久国产精品首页 | 爱爱免费视频 | 精品国产99 | 久久久久国产一区二区三区四区 | 国产激情91久久精品导航 | 亚洲精品一区中文字幕 | 日本三级电影在线观看视频 | 久在草 | 91色综合|