Browse Source

Deprecate Form::validator().

Added Form::getValidator(), Form::setValidator().
ADmad 8 years ago
parent
commit
2e164345fc
2 changed files with 52 additions and 13 deletions
  1. 38 2
      src/Form/Form.php
  2. 14 11
      tests/TestCase/Form/FormTest.php

+ 38 - 2
src/Form/Form.php

@@ -113,6 +113,11 @@ class Form
      */
     public function validator(Validator $validator = null)
     {
+        deprecationWarning(
+            'Form::validator() is deprecated. ' .
+            'Use Form::getValidator()/setValidator() instead.'
+        );
+
         if ($validator === null && empty($this->_validator)) {
             $validator = $this->_buildValidator(new $this->_validatorClass);
         }
@@ -124,6 +129,37 @@ 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
@@ -146,7 +182,7 @@ class Form
      */
     public function validate(array $data)
     {
-        $validator = $this->validator();
+        $validator = $this->getValidator();
         $this->_errors = $validator->errors($data);
 
         return count($this->_errors) === 0;
@@ -231,7 +267,7 @@ class Form
         $special = [
             '_schema' => $this->schema()->__debugInfo(),
             '_errors' => $this->errors(),
-            '_validator' => $this->validator()->__debugInfo()
+            '_validator' => $this->getValidator()->__debugInfo()
         ];
 
         return $special + get_object_vars($this);

+ 14 - 11
tests/TestCase/Form/FormTest.php

@@ -45,18 +45,21 @@ class FormTest extends TestCase
      * Test validator()
      *
      * @return void
+     * @deprecated
      */
     public function testValidator()
     {
-        $form = new Form();
-        $validator = $form->validator();
+        $this->deprecated(function () {
+            $form = new Form();
+            $validator = $form->validator();
 
-        $this->assertInstanceOf('Cake\Validation\Validator', $validator);
-        $this->assertSame($validator, $form->validator(), 'Same instance each time');
+            $this->assertInstanceOf('Cake\Validation\Validator', $validator);
+            $this->assertSame($validator, $form->validator(), 'Same instance each time');
 
-        $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
-        $this->assertSame($validator, $form->validator($validator));
-        $this->assertSame($validator, $form->validator());
+            $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
+            $this->assertSame($validator, $form->validator($validator));
+            $this->assertSame($validator, $form->validator());
+        });
     }
 
     /**
@@ -67,7 +70,7 @@ class FormTest extends TestCase
     public function testValidate()
     {
         $form = new Form();
-        $form->validator()
+        $form->getValidator()
             ->add('email', 'format', ['rule' => 'email'])
             ->add('body', 'length', ['rule' => ['minLength', 12]]);
 
@@ -94,7 +97,7 @@ class FormTest extends TestCase
     public function testErrors()
     {
         $form = new Form();
-        $form->validator()
+        $form->getValidator()
             ->add('email', 'format', [
                 'message' => 'Must be a valid email',
                 'rule' => 'email'
@@ -141,7 +144,7 @@ class FormTest extends TestCase
         $form = $this->getMockBuilder('Cake\Form\Form')
             ->setMethods(['_execute'])
             ->getMock();
-        $form->validator()
+        $form->getValidator()
             ->add('email', 'format', ['rule' => 'email']);
         $data = [
             'email' => 'rong'
@@ -162,7 +165,7 @@ class FormTest extends TestCase
         $form = $this->getMockBuilder('Cake\Form\Form')
             ->setMethods(['_execute'])
             ->getMock();
-        $form->validator()
+        $form->getValidator()
             ->add('email', 'format', ['rule' => 'email']);
         $data = [
             'email' => 'test@example.com'