title: Laravel - Validation - Require if field is null
body: <p>Let's say I have this html code:</p>
  
  <pre><code>&lt;input type="email" name="email"&gt;
  &lt;input type="password" name="password"&gt;
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>&lt;input type="hidden" name="user_id" value="1"&gt;
  </code></pre>
  
  <p>(If the user is logged in, only the <code>user_id</code> field will be shown. else - the credentials fields).</p>
  
  <p>When I create a request, there's a validation that checks the user. if the field <code>user_id</code> exists (i.e if <code>user_id</code> exists in the users table), then there's no need to require <code>email</code> and <code>password</code> inputs. If there's no <code>user_id</code>, then the <code>email</code> and <code>password</code> fields will be required.</p>
  
  <p>In other words, I want to do something like this:</p>
  
  <pre><code>public function rules()
  {
      return [
          'user_id'   =&gt; 'exists:users,id',
          'email'     =&gt; 'required_if_null:user_id|email|...',
          'password'  =&gt; 'required_if_null:user_id|...'
      ];
  }
  </code></pre>