简体   繁体   中英

how to use required_if validation in Laravel?

I really wish to know how am i supposed to use required_if on JSON ?

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.

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:

  1. For Request Schedule:

  2. request-type

  3. start-date

  4. end-date

  5. reason ( Mandatory if request type Request Day Off )

  6. route ( Mandatory if request type Request Schedule )

  7. actual-schedule (Mandatory if request type Change Schedule)

  8. change-to-schedule (Mandatory if request type Change Schedule)

  9. swap-crew-id (Mandatory if request type Swap Schedule)

  10. swap-crew-schedule (Mandatory if request type Swap Schedule)

  11. 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.

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

You just need to remove the 'white space' after ==, and pass $request object to the validate() method. 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'
]);

Demo

I think you didnt passed $request object to validate function

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'
    ]);
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM