0

I need to have validation on a field called address and that only gets validated if a checkbox field is not checked - so I know that when you don't check a checkbox it doesn't go in the post request. So how do I make address required_if chkaddress is not there in the post request? That's what I need to put in my validation rule could someone help me with this, please.

what I have is this;

    $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'
]);
ONYX
  • 5,679
  • 15
  • 83
  • 146

1 Answers1

0

Try with required_with:foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present.

'address' => 'required_with:chk_address',

edit: Sorry, is required_without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are not present.

'address' => 'required_without:chk_address',
porloscerros Ψ
  • 4,808
  • 2
  • 11
  • 20