[英]how to use required_if validation in Laravel?
I really wish to know how am i supposed to use required_if
on JSON
? 我真的很想知道我应该如何在
JSON
上使用required_if
?
I'm still confused about the idea of API but I have to finish my task, I've added the validation
on my controller but it doesn't work. 我仍然对API的概念感到困惑,但是我必须完成我的任务,我已经在控制器上添加了
validation
,但是它不起作用。
public function StoreReqSchedule(Request $request){
$this->validate([
'reqtype'=> 'required',
'startdate' => 'required',
'enddate' => 'required',
'reason'=> 'required_if: reqtype,==, Request Day Off',
'route' => 'required_if: reqtype,==, Request Schedule',
'actualschedule' => 'required_if:reqtype,==, Change Schedule',
'changetoschedule'=>'required_if:reqtype,==,Change schedule',
'swapcrewid'=> 'required_if:reqtype,==,Swap Schedule',
'swapcrewschedule'=>'required_if:reqtype,==,Swap Schedule',
'note'=>'required'
]);
// ...
}
is this the correct way to use required if? 这是所需的正确使用方法吗?
****EDIT** ****编辑**
Each API endpoint
consist of this parameter: 每个
API endpoint
包含以下参数:
For Request Schedule: 对于请求时间表:
request-type 请求类型
start-date 开始日期
end-date 结束日期
reason ( Mandatory if request type Request Day Off ) 原因( 如果请求类型为“请求日O ff”,则为必需 )
route ( Mandatory if request type Request Schedule ) 路线( 如果请求类型为“请求时间表”,则为强制 )
actual-schedule (Mandatory if request type Change Schedule) 实际时间表(如果请求类型更改时间表,则为必填)
change-to-schedule (Mandatory if request type Change Schedule) 更改时间表(如果请求类型为更改时间表,则为必填)
swap-crew-id (Mandatory if request type Swap Schedule) swap-crew-id(如果请求类型为“交换计划”,则必须提供)
swap-crew-schedule (Mandatory if request type Swap Schedule) swap-crew-schedule(如果请求类型为“ Swap Schedule”,则为必需)
note 注意
those lists above is the task, I'm having difficulties to understand how to write the code for Mandatory if request type Request Day Off and the other mandatory ofc. 上面的那些列表是任务, 如果请求类型为Request Day O ff和其他强制性ofc,我很难理解如何编写强制性代码。
ErrorException: Declaration of App\\Http\\Controllers\\B777Controller::validate($request) should be compatible with App\\Http\\Controllers\\Controller::validate(Illuminate\\Http\\Request $request, array $rules, array $messages = Array, array $customAttributes = Array) >> this is the error
ErrorException:App \\ Http \\ Controllers \\ B777Controller :: validate($ request)的声明应与App \\ Http \\ Controllers \\ Controller :: validate(Illuminate \\ Http \\ Request $ request,数组$ rules,数组$ messages = Array兼容,array $ customAttributes = Array)>>这是错误
You just need to remove the 'white space' after ==,
and pass $request
object to the validate()
method. 您只需要删除
==,
之后的“空白” ==,
然后将$request
对象传递给validate()
方法。 See below: 见下文:
$this->validate($request, [
'reqtype'=> 'required',
'startdate' => 'required',
'enddate' => 'required',
'reason'=> 'required_if:reqtype,==,Request Day Off',
'route' => 'required_if:reqtype,==,Request Schedule',
'actualschedule' => 'required_if:reqtype,==,Change Schedule',
'changetoschedule'=>'required_if:reqtype,==,Change schedule',
'swapcrewid'=> 'required_if:reqtype,==,Swap Schedule',
'swapcrewschedule'=>'required_if:reqtype,==,Swap Schedule',
'note'=>'required'
]);
I think you didnt passed $request object to validate function 我认为您没有通过$ request对象来验证功能
public function StoreReqSchedule(Request $request){
$this->validate($request,[
'reqtype'=> 'required',
'startdate' => 'required',
'enddate' => 'required',
'reason'=> 'required_if: reqtype,==, Request Day Off',
'route' => 'required_if: reqtype,==, Request Schedule',
'actualschedule' => 'required_if:reqtype,==, Change Schedule',
'changetoschedule'=>'required_if:reqtype,==,Change schedule',
'swapcrewid'=> 'required_if:reqtype,==,Swap Schedule',
'swapcrewschedule'=>'required_if:reqtype,==,Swap Schedule',
'note'=>'required'
]);
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.