Laravel "At Least and At Most" 需要一个字段
Laravel "At Least and At Most" one field is required
我有四个不同的字段 a、b、c 和 d。我只想填满其中一个。我该怎么做?
我在 Laravel 文档中找到了 required_without_all:foo,bar,...
,但这似乎至少允许一个字段。我也可以填充其他人。我只想填写一个字段,其中一个应该是必需的。
这是我目前所做的,但我可以将这些字段一起发送。
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"])
],
];
}
您的表单似乎包含四种类型住房的四个复选框。如果您需要恰好提供其中一个选项,为什么不使用单选按钮或 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>
或 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>
那么验证应该是这样的:
[
'housingtype' => 'required|in:flat,detachedHouse,terrace,semiDetached',
// ...
]
一个额外的好处:现在添加一种住房类型是微不足道的!
试试这个
$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');
}
}
],
]);
替换text1
、text2
和text3
。
当然你可以在function
之后的每个text
中添加更多的验证。您可以验证是字符串还是数字等...例如,在函数写入“size:3”之后,如果您写入单个字符
,这将return 出错
示例:
//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"]
将returns个错误:
- text1 必须是 3 个字符。
- 三个中只允许一个
我有四个不同的字段 a、b、c 和 d。我只想填满其中一个。我该怎么做?
我在 Laravel 文档中找到了 required_without_all:foo,bar,...
,但这似乎至少允许一个字段。我也可以填充其他人。我只想填写一个字段,其中一个应该是必需的。
这是我目前所做的,但我可以将这些字段一起发送。
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"])
],
];
}
您的表单似乎包含四种类型住房的四个复选框。如果您需要恰好提供其中一个选项,为什么不使用单选按钮或 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>
或 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>
那么验证应该是这样的:
[
'housingtype' => 'required|in:flat,detachedHouse,terrace,semiDetached',
// ...
]
一个额外的好处:现在添加一种住房类型是微不足道的!
试试这个
$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');
}
}
],
]);
替换text1
、text2
和text3
。
当然你可以在function
之后的每个text
中添加更多的验证。您可以验证是字符串还是数字等...例如,在函数写入“size:3”之后,如果您写入单个字符
示例:
//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"]
将returns个错误:
- text1 必须是 3 个字符。
- 三个中只允许一个