問題描述
我在 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ù)選框...)
我的情況是這樣的:
如果我從復(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)!