Friday, March 17, 2017

laravel validator (required_with_all) chaining

I use this validator inside my controller

    $validator = Validator::make($request->all(), [
            'host' => 'required_with_all:ttl,type,destination|alpha_num|max:32|nullable',
            'ttl' => 'required_with_all:host,type,destination|integer|between:3000,4800|nullable',
            'type' => 'required_with_all:host,ttl,destination|integer|between:1,5|nullable',
            'destination' => 'required_with_all:host,ttl,type|ip|max:64|nullable',
        ]);

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    dd("valid");

As you may see either all fields are required and be valid or all of the fields need to be empty.

How to make the validator allow either completely empty form inputs OR validate all of them (in case one of them contains input)?

Using the current code the validator doesn't validate the input, meaning only entering data into one field will return "valid" too.



via wichtel

Advertisement