[英]Laravel "At Least and At Most" one field is required
I have four different fields a, b, c and d.我有四个不同的字段 a、b、c 和 d。 I want only one of them to be filled.
我只想填满其中一个。 How may I do this?
我该怎么做?
I found out required_without_all:foo,bar,...
on Laravel documentation but this seems to allow at least one field.我在 Laravel 文档中发现了
required_without_all:foo,bar,...
但这似乎至少允许一个字段。 I can fill others as well.我也可以填充其他人。 I want only one field to be filled and one of them should be required.
我只想填写一个字段,其中一个应该是必需的。
This is what I did so far, but I am allowed to send these fields together.这是我到目前为止所做的,但我可以将这些字段一起发送。
public function rules()
{
return [
"isFlat" => "required_without_all:isDetachedHouse,isTerrace,isSemiDetached",
"isDetachedHouse" => "required_without_all:isFlat,isTerrace,isSemiDetached",
"isTerrace" => "required_without_all:isFlat,isDetachedHouse,isSemiDetached",
"isSemiDetached" => "required_without_all:isFlat,isDetachedHouse,isTerrace",
"finishQuality" => [
"required",
Rule::in(["luxury", "standard", "economy"])
],
];
}
Try this尝试这个
$validations = $request->validate([
'text1' => [
'bail',
function ($attribute, $value, $fail) {
if (request()->filled('text2') && request()->filled("text3")) {
return $fail('Only 1 of the three is allowed');
}
}
],
"text2" => [
'bail',
function ($attribute, $value, $fail) {
if (request()->filled('text1') && request()->filled("text3")) {
return $fail('Only 1 of the three is allowed');
}
}
],
"text3" => [
'bail',
function ($attribute, $value, $fail) {
if (request()->filled('text1') && request()->filled("text2")) {
return $fail('Only 1 of the three is allowed');
}
if (!request()->filled('text1') && !request()->filled("text2") && !request()->filled($attribute)) {
return $fail('You must fill one');
}
}
],
]);
Replace text1
, text2
and text3
.替换
text1
、 text2
和text3
。
Of course you can add more validation to each text
after function
.当然,您可以在
function
之后为每个text
添加更多验证。 You can validate if is string or number, etc... for example after function write "size:3" this will return error if you write a single character您可以验证是字符串还是数字等...例如在 function 之后写入“size:3” 如果您写入单个字符,这将返回错误
Example:例子:
//validation
'text1' => [
'bail',
function ($attribute, $value, $fail) {
if (request()->filled('text2') && request()->filled("text3")) {
return $fail('Only 1 of the three is allowed');
}
},
"size:3"
],
// I fill form with these values
text1[value="q"]
text2[value=null]
text3[value="hey"]
Will returns errors:将返回错误:
This looks like you have a form with four checkboxes for the four types of housing.看起来您有一个表单,其中包含四种类型住房的四个复选框。 If you require that exactly one of those options is provided, why not use a radio-button or a select?
如果您需要提供其中一个选项,为什么不使用单选按钮或 select?
<label>
<input type="radio" name="housingtype" value="flat">
flat
</label>
<label>
<input type="radio" name="housingtype" value="detachedHouse">
detached house
</label>
<label>
<input type="radio" name="housingtype" value="terrace">
terrace
</label>
<label>
<input type="radio" name="housingtype" value="semiDetached">
semi-detached
</label>
Or with a select:或者拨打 select:
<select name="housingtype">
<option value="flat">flat</option>
<option value="detachedHouse">detached house</option>
<option value="terrace">terrace</option>
<option value="semiDetached">semi-detached</option>
</select>
Then the validation should be like:然后验证应该是这样的:
[
'housingtype' => 'required|in:flat,detachedHouse,terrace,semiDetached',
// ...
]
One extra benefit: it is now trivial to add a housing type!一个额外的好处:现在添加一种住房类型是微不足道的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.