Laravel-将数组验证为现有数组并为空

Laravel-将数组验证为现有数组并为空,laravel,Laravel,当前正在为重复计划创建API端点,但我在验证数组以确保其存在于预设数组中时遇到问题: 例如,repeat_day值必须具有正确的天数 这是API请求: { "repeat_by": "daily", "repeat_day": [ "Sunday", "xxx" ], "repeat_date": [], "repeat_week": [], "repeat_month": [] } 这是StoreUpdateScheduleR

当前正在为重复计划创建API端点,但我在验证数组以确保其存在于预设数组中时遇到问题:

例如,
repeat_day
值必须具有正确的天数

这是API请求:

{
   "repeat_by": "daily",
   "repeat_day": [
       "Sunday",
       "xxx" 
   ],
   "repeat_date": [],
   "repeat_week": [],
   "repeat_month": []
}
这是
StoreUpdateScheduleRequest

public function\uuuu construct()
{
$this->dayArray=[“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”];
//和其他一些数组
}
公共职能规则()
{
$repeatDayRule=request()->get('repeat_by')=“每日”['required',Rule::in($this->dayArray)]:'';
//以及其他一些验证规则
返回[
“repeat_day.*”=>$repeatDayRule
“repeat_date.*”=>$repeatDateRule
“重复一周。*”=>$repeatWeekRule
“重复月。*”=>$repeatMonthRule
];
}
我的问题是,当我提供
null
或空数组时,验证通过了它应该失败的部分

{
    "repeat_by": "daily"
    "repeat_day": null //or even []
}

注:

  • 我尝试对
    request\u day
    进行验证,而不是
    request\u day.
    ,但它无法正确验证该天

  • 希望使用Laravel内置验证(可能我遗漏了一些东西),而不希望扩展验证



提前谢谢!干杯

在使用规则验证该数组时,应该实现这两个规则。大概是这样的:

返回[
“repeat_day”=>“array | filled”,
“repeat_day.*”=>$repeatDayRule,
“repeat_date”=>“array | filled”,
“repeat_date.*”=>$repeatDateRule,
“重复_周”=>“数组已填充”,
“重复一周。*”=>$repeatWeekRule,
“repeat_month”=>“array | filled”,
“重复月。*”=>$repeatMonthRule
];

使用此代码,您将检查,例如,
repeat\u day
是一个数组,并且它是填充的。在下一句中,您将使用
$repeatDayRule

'repeat\u day'=>'nullable'检查其值是否有效


在您的请求验证中插入此项

验证应失败,这将导致它接受空值,目前这种情况已经存在