Browse Source

Fixed Form regression that skipped validation

Jeremy Harris 8 years ago
parent
commit
a061a58706
3 changed files with 41 additions and 0 deletions
  1. 3 0
      src/Form/Form.php
  2. 11 0
      tests/TestCase/Form/FormTest.php
  3. 27 0
      tests/test_app/TestApp/Form/ValidateForm.php

+ 3 - 0
src/Form/Form.php

@@ -223,6 +223,9 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
     public function validate(array $data)
     {
         $validator = $this->getValidator();
+        if (!$validator->count()) {
+            $validator = $this->validator();
+        }
         $this->_errors = $validator->errors($data);
 
         return count($this->_errors) === 0;

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

@@ -19,6 +19,7 @@ use Cake\TestSuite\TestCase;
 use Cake\Validation\Validator;
 use TestApp\Form\AppForm;
 use TestApp\Form\FormSchema;
+use TestApp\Form\ValidateForm;
 
 /**
  * Form test case.
@@ -126,6 +127,16 @@ class FormTest extends TestCase
         $this->assertCount(0, $form->errors());
     }
 
+    public function testValidateDeprected()
+    {
+        $this->deprecated(function() {
+            $form = new ValidateForm();
+            $data = [];
+            $this->assertFalse($form->validate($data));
+            $this->assertCount(1, $form->errors());
+        });
+    }
+
     /**
      * Test the errors methods.
      *

+ 27 - 0
tests/test_app/TestApp/Form/ValidateForm.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Form;
+
+use Cake\Form\Form;
+
+class ValidateForm extends Form
+{
+
+    public function validator(\Cake\Validation\Validator $validator = null)
+    {
+        return parent::validator($validator)
+            ->requirePresence('title');
+    }
+}