title: On the fly / dynamic CakePhp 3 Validation and FormHelper
body: <p>I am creating a 'form editor' in CakePHP.</p>
  
  <p>The interface allows the user to choose validations to apply to the fields, e.g. Numeric, Email etc.</p>
  
  <p>As such I need to dynamically create validation for the model based on the user input. For this I can use the Validation object: <a href="https://book.cakephp.org/3.0/en/core-libraries/validation.html" rel="nofollow noreferrer">https://book.cakephp.org/3.0/en/core-libraries/validation.html</a></p>
  
  <p>I want to take advantage of the features of FormHelper, for example, automatically outputting error messages for fields.</p>
  
  <p>I can see how to use a hard-coded validator from the model to do this by setting the validator in the context option for Form->create() - but how do I use a customer $Validator object which has been dynamically created?</p>
  
  <p>Clarification:</p>
  
  <p>I have some code in my controller:</p>
  
  <pre><code>//get the form configuration
  $form = $this-&gt;Forms-&gt;get($id, ['contain'=&gt;['FormFields'=&gt;['Validations']]]);
  
  
  //set up validation based on the configuration
  
  $validator = new Validator();
  foreach($form-&gt;form_fields as $field){
    ...
    if($field-&gt;required) $validator-&gt;notBlank($field-&gt;field_name);
  
  }
  
  
  $table = TableRegistry::get($form-&gt;type);
  
  $table-&gt;setValidator($validator);
  </code></pre>
  
  <p>Unfortunately setValidator() is not a method of TableRegistry.</p>
  
  <p>If I set the validation up in the model, I would need the $id parameter to look up the correct form configuration.</p>