Browse Source

Make Form use ValidatorAwareTrait.

ADmad 8 years ago
parent
commit
ee3c4a793c
2 changed files with 78 additions and 37 deletions
  1. 58 35
      src/Form/Form.php
  2. 20 2
      tests/TestCase/Form/FormTest.php

+ 58 - 35
src/Form/Form.php

@@ -14,7 +14,14 @@
  */
 namespace Cake\Form;
 
+use Cake\Event\Event;
+use Cake\Event\EventDispatcherInterface;
+use Cake\Event\EventDispatcherTrait;
+use Cake\Event\EventListenerInterface;
+use Cake\Event\EventManager;
 use Cake\Validation\Validator;
+use Cake\Validation\ValidatorAwareInterface;
+use Cake\Validation\ValidatorAwareTrait;
 
 /**
  * Form abstraction used to create forms not tied to ORM backed models,
@@ -33,9 +40,26 @@ use Cake\Validation\Validator;
  *
  * Forms are conventionally placed in the `App\Form` namespace.
  */
-class Form
+class Form implements EventListenerInterface, EventDispatcherInterface, ValidatorAwareInterface
 {
 
+    use EventDispatcherTrait;
+    use ValidatorAwareTrait;
+
+    /**
+     * The alias this object is assigned to validators as.
+     *
+     * @var string
+     */
+    const VALIDATOR_PROVIDER_NAME = 'form';
+
+    /**
+     * The name of the event dispatched when a validator has been built.
+     *
+     * @var string
+     */
+    const BUILD_VALIDATOR_EVENT = 'Form.buildValidator';
+
     /**
      * The schema used by this form.
      *
@@ -58,11 +82,29 @@ class Form
     protected $_validator;
 
     /**
-     * Validator class.
+     * Constructor
+     */
+    public function __construct()
+    {
+        $this->_eventManager = new EventManager();
+        $this->_eventManager->on($this);
+    }
+
+    /**
+     * Get the Form callbacks this form is interested in.
      *
-     * @var string
+     * The conventional method map is:
+     *
+     * - Form.buildValidator => buildValidator
+     *
+     * @return array
      */
-    protected $_validatorClass = 'Cake\Validation\Validator';
+    public function implementedEvents()
+    {
+        return [
+            'Form.buildValidator' => 'buildValidator',
+        ];
+    }
 
     /**
      * Get/Set the schema for this form.
@@ -129,37 +171,6 @@ class Form
     }
 
     /**
-     * Get the validator for this form.
-     *
-     * This method will call `_buildValidator()` when the validator
-     * is first built. This hook method lets you configure the
-     * validator or load a pre-defined one.
-     *
-     * @return \Cake\Validation\Validator the validator instance.
-     */
-    public function getValidator()
-    {
-        if (empty($this->_validator)) {
-            $this->_validator = $this->_buildValidator(new $this->_validatorClass);
-        }
-
-        return $this->_validator;
-    }
-
-    /**
-     * Set a custom validator instance.
-     *
-     * @param \Cake\Validation\Validator $validator Validator object to be set.
-     * @return $this
-     */
-    public function setValidator(Validator $validator)
-    {
-        $this->_validator = $validator;
-
-        return $this;
-    }
-
-    /**
      * A hook method intended to be implemented by subclasses.
      *
      * You can use this method to define the validator using
@@ -175,6 +186,18 @@ class Form
     }
 
     /**
+     * Callback method for Form.buildValidator event.
+     *
+     * @param \Cake\Event\Event $event The Form.buildValidator event instance.
+     * @param \Cake\Validation\Validator $validator The validator to customize.
+     * @param string $name Validator name
+     * @return void
+     */
+    public function buildValidator(Event $event, Validator $validator, $name)
+    {
+    }
+
+    /**
      * Used to check if $data passes this form's validation.
      *
      * @param array $data The data to check.

+ 20 - 2
tests/TestCase/Form/FormTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\Form;
 
 use Cake\Form\Form;
 use Cake\TestSuite\TestCase;
+use Cake\Validation\Validator;
 
 /**
  * Form test case.
@@ -63,6 +64,23 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test getValidator()
+     *
+     * @return void
+     */
+    public function testGetValidator()
+    {
+        $form = $this->getMockBuilder(Form::class)
+            ->setMethods(['buildValidator'])
+            ->getMock();
+
+        $form->expects($this->once())
+            ->method('buildValidator');
+
+        $this->assertInstanceof(Validator::class, $form->getValidator());
+    }
+
+    /**
      * Test setValidator()
      *
      * @return void
@@ -70,9 +88,9 @@ class FormTest extends TestCase
     public function testSetValidator()
     {
         $form = new Form();
-
         $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
-        $form->setValidator($validator);
+
+        $form->setValidator('default', $validator);
         $this->assertSame($validator, $form->getValidator());
     }