Search code examples
laravellaravel-5laravel-5.3

required field if other field doesn't have specific value


I have a field project which is required if account_name field doesn't have the value '0'. I want to validate this scenario in laravel

eg if account_name = '0'  -project_name field is not required 
   if account_name = '1'    - project_name field is required 


 $this->validate($request,[
     'project_name'=>'required_without:account_name,0',
    ]); 

Solution

  • Try to use required_unless! Then the name will be required unless account_name equals 0.

     $this->validate($request,[
         'project_name'=>'required_unless:account_name,0',
        ]);