【问题标题】:laravel validation rule for checkbox not checked required if如果不需要检查复选框的laravel验证规则
【发布时间】:2019-06-04 12:41:42
【问题描述】:

我需要对名为 address 的字段进行验证,并且只有在未选中复选框字段时才会验证 - 所以我知道当您不选中复选框时,它不会进入发布请求。那么我如何使地址 required_if chkaddress 在 post 请求中不存在?这就是我需要在验证规则中添加的内容,请有人帮助我。

我有的是这个;

    $validator = Validator::make($request->all(), [
    'owner_firstname'       => 'required|min:2|max:30',
    'owner_lastname'        => 'required|min:2|max:30',
    'partner_firstname'     => 'nullable|min:2|max:30',
    'partner_lastname'      => 'nullable|min:2|max:30',
    'baby_firstname'        => 'nullable|min:2|max:30',
    'baby_lastname'         => 'nullable|min:2|max:30',
    'month'                 => 'not_in:0',
    'day'                   => 'not_in:0',
    'year'                  => 'numeric|digits:4',
    'guests_message'        => 'required|min:30|max:5000',
    'address'               => 'required_unless:chk_address,off', // this line here
    'fullname'              => 'required_if:chk_address,on',
    'address_line_01'       => 'required_if:chk_address,on',
    // 'address_line_02'        => 'required_if:chk_address,on',
    'city'                  => 'required_if:chk_address,on',
    'state'                 => 'required_if:chk_address,on',
    'zip_code'              => 'required_if:chk_address,on',
    'phone'                 => 'required_if:chk_address,on',
    'country'               => 'required_if:chk_address,on|not_in:0'
]);

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    试试required_with:foo,bar,...

    验证中的字段必须存在且不为空(如果有) 的其他指定字段。

    'address' => 'required_with:chk_address',
    

    编辑: 对不起,是required_without:foo,bar,...

    验证中的字段必须存在且不为空,仅当有 的其他指定字段不存在。

    'address' => 'required_without:chk_address',
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2019-05-18
      • 1970-01-01
      相关资源
      最近更新 更多