久久久久久久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 | 性视频一区 | 亚洲狠狠 | 欧美一区二| 久久手机在线视频 | 国内久久| 一级特黄a大片 | 五月激情婷婷在线 | 欧美一级片中文字幕 | 日韩图区| 日韩精品久久一区 | 在线观看的av | 午夜资源 | 午夜免费视频 | 亚洲黄色在线 | 免费福利视频一区二区三区 | 亚洲高清在线免费观看 | 91视频进入| 午夜在线视频 | 日韩三级电影一区二区 | 成人黄色网址大全 | 中文字幕日本一区二区 | www.97zyz.com| 日韩一二区 | 91精品国产91久久久久久三级 | av日韩在线播放 | 人人干人人草 | h视频在线免费 | 久久国产成人午夜av影院武则天 | 特级生活片 | 国产伦精品一区二区三区照片91 | 中文天堂在线一区 | 激情黄色在线观看 | 高清欧美性猛交 | 欧美精品久久久久久久久老牛影院 | 国产一极毛片 | 国产激情一区二区三区 | 国产香蕉视频在线播放 | 麻豆成人在线视频 |