简体   繁体   中英

Laravel multiple fields validation not working

I'm trying to validate three input fields.

If the fields are null then the three fields are not mandatory, but If at least one of them is not empty (null) then all three should be mandatory. But It's doesn't working. If I leave all three fields blank It shows that the three fields are mandatory. It shoud allow the form to be submitted.

public function rules()
    {
      return [
        'a' => 'required_if:b,!=,null|required_if:c,!=,null',
        'b' => 'required_if:a,!=,null|required_if:c,!=,null',
        'c' => 'required_if:a,!=,null|required_if:b,!=,null',
      ];
    }

Dale suggests using required_with instead of required_if , however you can use Preparing Input For Validation it will do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM