我需要使用带有in:1的required_id,因为只有当另一个字段等于4时才需要检查此字段
'affirm_agency' => 'required_if:role,4|in:1',
但是当我这样写的时候,我不能通过验证,当它不等于1时
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class userRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
public function rules()
{
return [ 'affirm_agency' => 'required_if:role,4|in:1',];
}
}
答案 0 :(得分:1)
我通常像这样处理这样的事情:
/**
* Validation rules
*
* @return array
*/
public function rules() : array
{
$rules = [
// add whatever default rules you always want
];
if (request()->role == 4) {
$rules['affirm_agency'] = 'required|in:1';
}
return $rules;
}
答案 1 :(得分:0)
Please see laravel documentation
在:FOO,酒吧,...
验证字段必须包含在给定的值列表中。由于此规则通常要求您内爆数组,因此可以使用Rule :: in方法流畅地构造规则:
use Illuminate\Validation\Rule;
Validator::make($data, [
'zones' => [
'required',
Rule::in(['first-zone', 'second-zone']),
],
]);
您需要更新授权功能,因为它现在返回false。
public function authorize()
{
return true;
}