Browse Source

Add docs and constant for defining validator hookups.

Document how the trait interacts with the including class, and define
a constant so that not all validator aware things get assigned as
'table' to the validators.
Mark Story 11 years ago
parent
commit
b02e89a102
2 changed files with 17 additions and 1 deletions
  1. 7 0
      src/ORM/Table.php
  2. 10 1
      src/Validation/ValidatorAwareTrait.php

+ 7 - 0
src/ORM/Table.php

@@ -131,6 +131,13 @@ class Table implements RepositoryInterface, EventListenerInterface
     const DEFAULT_VALIDATOR = 'default';
 
     /**
+     * The alias this object is assigned to validators as.
+     *
+     * @var string
+     */
+    const VALIDATOR_PROVIDER_NAME = 'table';
+
+    /**
      * Name of the table as it can be found in the database
      *
      * @var string

+ 10 - 1
src/Validation/ValidatorAwareTrait.php

@@ -23,6 +23,15 @@ use Cake\Validation\Validator;
  * This trait is useful when building ORM like features where
  * the implementing class wants to build and customize a variety
  * of validator instances.
+ *
+ * This trait expects that classes including it define two constants:
+ *
+ * - `DEFAULT_VALIDATOR` - The default validator name.
+ * - `VALIDATOR_PROVIDER_NAME ` - The provider name the including class is assigned
+ *   in validators.
+ *
+ * If the including class also implements events the `Model.buildValidator` event
+ * will be triggered when validators are created.
  */
 trait ValidatorAwareTrait
 {
@@ -89,7 +98,7 @@ trait ValidatorAwareTrait
             }
         }
 
-        $validator->provider('table', $this);
+        $validator->provider(self::VALIDATOR_PROVIDER_NAME, $this);
         return $this->_validators[$name] = $validator;
     }