required_if Laravel 5 validation

前端 未结 4 1360
悲&欢浪女
悲&欢浪女 2020-12-02 22:11

I have form that a user can fill-out for selling their home. And for one of the in puts, a user must select weather it will be \"For Sale\" or \"For Rent\". If it is For Sal

4条回答
  •  隐瞒了意图╮
    2020-12-02 23:07

    You can use Illuminate\Validation\Rule in Laravel as given below to build the validator.

    $validator =  Validator::make( $request->input(), [
        'list_type' => 'required',
        'sale_price'=> Rule::requiredIf( function () use ($request){
            return $request->input('list_type') == 'For Sale';
        }),
        'rent_price'=> Rule::requiredIf( function () use ($request){
            return $request->input('list_type') == 'For Rent';
        }),
    ]);
    

提交回复
热议问题