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

如果值存在于另一個字段數組中,則 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 驗證規則的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
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)
主站蜘蛛池模板: 午夜精品久久久久久久久久久久久 | 久久久久亚洲 | 亚洲视频在线观看一区二区三区 | 男女视频在线免费观看 | 九九久久久 | 久久久久国产 | 日韩视频三区 | 91久久精品一区二区二区 | 久久精品国产一区二区三区不卡 | 免费黄色成人 | 欧美精品一区二区三区在线 | 精品国产乱码久久久久久88av | www.欧美视频| 国产精品二区三区在线观看 | 羞羞午夜 | 欧美一区二区三区一在线观看 | 亚洲成人午夜电影 | 综合久久综合久久 | 成人一区二区视频 | 亚洲不卡在线观看 | 日韩久久精品电影 | 亚洲国产黄色av | 久久91av | 一区二区三区小视频 | 久久久精品网站 | 日韩二区| 成人国产精品入口免费视频 | 亚洲欧美精品在线观看 | 日本超碰 | 亚洲高清网| 国产乱xxav | 波多野结衣精品在线 | 亚洲视频免费观看 | 亚洲综合在线网 | 中文字幕在线免费观看 | 黄色片免费看 | 国产高清91| 久久久久久91 | 久久久久精 | 美女久久| 男人天堂网av |