ソースを参照

Form::validate() accept object validator.

Erwane Breton 4 年 前
コミット
03fe1227c2
2 ファイル変更18 行追加0 行削除
  1. 3 0
      src/Form/Form.php
  2. 15 0
      tests/TestCase/Form/FormTest.php

+ 3 - 0
src/Form/Form.php

@@ -224,6 +224,9 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
             $validator = $this->getValidator();
         } elseif (is_string($options['validate'])) {
             $validator = $this->getValidator($options['validate']);
+        } elseif (is_object($options['validate'])) {
+            /** @var \Cake\Validation\Validator $validator */
+            $validator = $options['validate'];
         }
 
         $this->_errors = $validator->validate($data);

+ 15 - 0
tests/TestCase/Form/FormTest.php

@@ -140,6 +140,21 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test validate with object validator
+     */
+    public function testValidateObjectValidator(): void
+    {
+        $form = new Form();
+
+        $validator = clone $form->getValidator();
+        $validator->add('email', 'format', ['rule' => 'email']);
+
+        $data = ['email' => 'wrong'];
+
+        $this->assertFalse($form->validate($data, ['validate' => $validator]));
+    }
+
+    /**
      * Test the get errors methods.
      */
     public function testGetErrors(): void