laravel复选框的验证规则未选中,如果

时间:2019-06-04 04:23:12

标签: php laravel

我需要在名为address的字段上进行验证,并且只有在未选中复选框字段的情况下才会得到验证-因此我知道,当您不选中复选框时,它不会出现在发布请求中。那么,如果发帖请求中没有chkaddress,我该如何设置地址为required_?这就是我需要输入的验证规则,请有人帮助我。

我有什么;

    $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'
]);

1 个答案:

答案 0 :(得分:0)

尝试使用required_with:foo,bar,...

  

正在验证的字段必须存在,并且仅在有任何字段时不为空   还有其他指定字段。

'address' => 'required_with:chk_address',

编辑: 抱歉,是required_without:foo,bar,...

  

正在验证的字段必须存在,并且只有在任何情况下都不能为空   其他指定字段不存在。

'address' => 'required_without:chk_address',