dspld86684 2015-04-13 15:58
浏览 41
已采纳

如何在laravel中使用multi required_if?

1) I am new to laravel and want to integrate validation rules. My requirement is to make third field mandatory on basis of two other fields. Field C is required if both a and b are true. I have used required_if to put validation on basis of other single field but how can i use required_if to check two fields?

2) To achieve above functionality i tried custom validation rule as well. But it's working only if i will pull required rule alongwith.

For example:

'number_users' => 'required|custom_rule'   //working 
'number_users' => 'custom_rule'   //Not working
  • 写回答

4条回答 默认 最新

  • doubu7425 2015-04-26 19:55
    关注

    You can use conditional rules for that.

    Here's a simple example:

    $input = [
        'a' => true,
        'b' => true,
        'c' => ''
    ];
    
    $rules = [
        'a' => 'required',
        'b' => 'required'
        // specify no rules for c, we'll do that below
    ];
    
    $validator = Validator::make($input, $rules);
    
    // now here's where the magic happens
    $validator->sometimes('c', 'required', function($input){
        return ($input->a == true && $input->b == true);
    });
    
    dd($validator->passes()); // false in this case
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程