title: Laravel validation: validate field only if another is present
body: <p>I wanted to validate an 'account_id' only if 'needs_login' is present. I did this:</p>
  
  <pre><code>$rules = [
      'account_id' =&gt; ['required_with:needs_login','custom_validation']
  ];
  </code></pre>
  
  <p>But it doesn't work, because if needs_login field is not present but account_id has some value, then it tries to do the 'custom_validation'.
  I also tried to put the 'sometimes' parameter</p>
  
  <pre><code>$rules = [
      'account_id' =&gt; ['required_with:needs_login', 'sometimes', 'custom_validation']
  ];
  </code></pre>
  
  <p>but it didn't work.</p>
  
  <p>Any ideas?</p>
  
  <blockquote>
    <p><strong>P.S.:</strong> Remember that I wanted to validate the account_id only if needs_login is present, not to check if account_id is present if needs_login does.</p>
  </blockquote>