title: CakePHP: How can I invalidate form fields when hasMany association is used?
body: <p>I need to validate data in code as some validation rules depend on more than one field.</p>
  
  <p>My models are <code>Person</code>, <code>Address</code> &amp; <code>Hobby</code>. Associations look like this:</p>
  
  <pre><code>Person 
      hasOne Address
      hasMany Hobby
  
  Address
      belongsTo Person
  
  Hobby
      belongsTo Person
  </code></pre>
  
  <p>Now when any field of the <code>Person</code> model is not correct or any <code>Address</code> field is not correct I can simply write the following code in my controller to inform user he/she needs to correct given field:</p>
  
  <pre><code>// this works fine!
  $this-&gt;Person-&gt;invalidate('person_model_field_name', __('Help text'));
  $this-&gt;Person-&gt;Address-&gt;invalidate('address_model_field_name', __('Help text'));
  </code></pre>
  
  <p><strong>Now, how can I invalidate any of the <code>Hobby</code> model fields in a similar way?</strong></p>
  
  <p>I tried really hard last night and all my attempts failed. I cannot believe there is no way to do this in CakePHP v. 2.4.3 (after all this is not one of the first releases, right?).</p>
  
  <pre><code>// doesn't work
  $this-&gt;Person-&gt;Hobby-&gt;invalidate('hobby_model_field_name', __('Help text'));
  // doesn't work neither
  $this-&gt;Person-&gt;Hobby-&gt;invalidate('Hobby.0.hobby_model_field_name', __('Help text'));
  // tried to work directly on validationErrors array but also failed
  </code></pre>
  
  <p>I've also seen <a href="https://stackoverflow.com/questions/19261277/what-is-the-simplest-way-to-display-validation-errors-for-hasmany-associations-i">this question</a> but I am still lost. :-(</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>To shed some light here is an example why I need this:</p>
  
  <p>For instance: when <code>Person.is_student</code> field is <code>true</code> user needs to fill in <code>Person.studies_year</code> field; otherwise he/she needs to fill in <code>Person.diploma_date</code> field. So in <code>Person</code> model validation rules for both <code>studies_year</code> and <code>diploma_date</code> I need to use <code>allowEmpty</code> rule. Similar case with Hobby model.</p>