Browse Source

Merge pull request #12601 from inoas/Form-Form-getErrors

deprecate \Cake\Form\Form\errors();
Marc Würth 7 years ago
parent
commit
df0cfb10ac

+ 3 - 3
src/Datasource/EntityTrait.php

@@ -1032,13 +1032,13 @@ trait EntityTrait
      * $entity->errors('salary', ['must be numeric', 'must be a positive number']);
      *
      * // Returns the error messages for a single field
-     * $entity->errors('salary');
+     * $entity->getErrors('salary');
      *
      * // Returns all error messages indexed by field name
-     * $entity->errors();
+     * $entity->getErrors();
      *
      * // Sets the error messages for multiple fields at once
-     * $entity->errors(['salary' => ['message'], 'name' => ['another message']);
+     * $entity->getErrors(['salary' => ['message'], 'name' => ['another message']);
      * ```
      *
      * When used as a setter, this method will return this entity instance for method

+ 20 - 1
src/Form/Form.php

@@ -251,9 +251,28 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
      * to `validate()` or `execute()`.
      *
      * @return array Last set validation errors.
+     * @deprecated 3.7.0 Use Form::getErrors() instead.
      */
     public function errors()
     {
+        deprecationWarning(
+            'Form::errors() is deprecated. ' .
+            'Use Form::getErrors() instead.'
+        );
+
+        return $this->getErrors();
+    }
+
+    /**
+     * Get the errors in the form
+     *
+     * Will return the errors from the last call
+     * to `validate()` or `execute()`.
+     *
+     * @return array Last set validation errors.
+     */
+    public function getErrors()
+    {
         return $this->_errors;
     }
 
@@ -353,7 +372,7 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
     {
         $special = [
             '_schema' => $this->schema()->__debugInfo(),
-            '_errors' => $this->errors(),
+            '_errors' => $this->getErrors(),
             '_validator' => $this->getValidator()->__debugInfo()
         ];
 

+ 1 - 1
src/Form/README.md

@@ -55,7 +55,7 @@ You can always define additional public methods as you need as well.
 ```php
 $contact = new ContactForm();
 $success = $contact->execute($data);
-$errors = $contact->errors();
+$errors = $contact->getErrors();
 ```
 
 ## Documentation

+ 1 - 1
src/View/Form/FormContext.php

@@ -209,6 +209,6 @@ class FormContext implements ContextInterface
      */
     public function error($field)
     {
-        return array_values((array)Hash::get($this->_form->errors(), $field, []));
+        return array_values((array)Hash::get($this->_form->getErrors(), $field, []));
     }
 }

+ 16 - 5
tests/TestCase/Form/FormTest.php

@@ -117,14 +117,14 @@ class FormTest extends TestCase
             'body' => 'too short'
         ];
         $this->assertFalse($form->validate($data));
-        $this->assertCount(2, $form->errors());
+        $this->assertCount(2, $form->getErrors());
 
         $data = [
             'email' => 'test@example.com',
             'body' => 'Some content goes here'
         ];
         $this->assertTrue($form->validate($data));
-        $this->assertCount(0, $form->errors());
+        $this->assertCount(0, $form->getErrors());
     }
 
     /**
@@ -140,7 +140,7 @@ class FormTest extends TestCase
 
             $data = [];
             $this->assertFalse($form->validate($data));
-            $this->assertCount(1, $form->errors());
+            $this->assertCount(1, $form->getErrors());
         });
     }
 
@@ -148,9 +148,20 @@ class FormTest extends TestCase
      * Test the errors methods.
      *
      * @return void
+     * @deprecated 3.7.0 Use Form::testGetErrors() instead.
      */
     public function testErrors()
     {
+        $this->testGetErrors();
+    }
+
+    /**
+     * Test the get errors methods.
+     *
+     * @return void
+     */
+    public function testGetErrors()
+    {
         $form = new Form();
         $form->getValidator()
             ->add('email', 'format', [
@@ -167,7 +178,7 @@ class FormTest extends TestCase
             'body' => 'too short'
         ];
         $form->validate($data);
-        $errors = $form->errors();
+        $errors = $form->getErrors();
         $this->assertCount(2, $errors);
         $this->assertEquals('Must be a valid email', $errors['email']['format']);
         $this->assertEquals('Must be so long', $errors['body']['length']);
@@ -186,7 +197,7 @@ class FormTest extends TestCase
         ];
 
         $form->setErrors($expected);
-        $this->assertSame($expected, $form->errors());
+        $this->assertSame($expected, $form->getErrors());
     }
 
     /**