Checking if at least one field is set using CakePHP 3 validation
<p>I am trying to write a validation rule in CakePHP 3 that checks, if the prename and lastname OR the company name is set.</p>

<p>Validators:</p>

<pre><code>$validator
    -&gt;add('prename', 'custom', [
        'rule' =&gt; [$this, 'validateName'],
        'message' =&gt; __('Prename and lastname OR company name must be set.')
    ]);

$validator
    -&gt;add('lastname', 'custom', [
        'rule' =&gt; [$this, 'validateName'],
        'message' =&gt; __('Prename and lastname OR company name must be set.')
    ]);

$validator
    -&gt;add('name', 'custom', [
        'rule' =&gt; [$this, 'validateName'],
        'message' =&gt; __('Prename and lastname OR company name must be set.')
    ]);
</code></pre>

<p>Rule definition:</p>

<pre><code>public function validateName($check, array $context)
{
    if((!empty($context['data']['prename']) &amp;&amp; !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
        return true;
    } else {
        return false;
    }
}
</code></pre>

<p>But the validation does not behave as expected. If I enter the company name, I get validation errors for prename and lastname saying that the fields is required. Same when I enter the prename and lastname, it says the company name is required.</p>

<p>What am I doing wrong?</p>