Browse Source

getErrors

inoas 7 years ago
parent
commit
45e50d00b9

+ 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, []));
     }
 }

+ 17 - 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());
         });
     }
 
@@ -151,6 +151,18 @@ class FormTest extends TestCase
      */
     public function testErrors()
     {
+        $this->deprecated(function () {
+            $this->testGetErrors();
+        });
+    }
+
+    /**
+     * Test the get errors methods.
+     *
+     * @return void
+     */
+    public function testGetErrors()
+    {
         $form = new Form();
         $form->getValidator()
             ->add('email', 'format', [
@@ -167,7 +179,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 +198,7 @@ class FormTest extends TestCase
         ];
 
         $form->setErrors($expected);
-        $this->assertSame($expected, $form->errors());
+        $this->assertSame($expected, $form->getErrors());
     }
 
     /**

+ 10 - 10
tests/TestCase/ORM/EntityTest.php

@@ -1183,30 +1183,30 @@ class EntityTest extends TestCase
     {
         $this->deprecated(function () {
             $entity = new Entity();
-            $this->assertEmpty($entity->errors());
-            $this->assertSame($entity, $entity->errors('foo', 'bar'));
-            $this->assertEquals(['bar'], $entity->errors('foo'));
+            $this->assertEmpty($entity->getErrors());
+            $this->assertSame($entity, $entity->getErrors('foo', 'bar'));
+            $this->assertEquals(['bar'], $entity->getErrors('foo'));
 
-            $this->assertEquals([], $entity->errors('boo'));
+            $this->assertEquals([], $entity->getErrors('boo'));
             $entity['boo'] = [
                 'something' => 'stupid',
                 'and' => false
             ];
-            $this->assertEquals([], $entity->errors('boo'));
+            $this->assertEquals([], $entity->getErrors('boo'));
 
             $entity->errors('foo', 'other error');
-            $this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
+            $this->assertEquals(['bar', 'other error'], $entity->getErrors('foo'));
 
             $entity->errors('bar', ['something', 'bad']);
-            $this->assertEquals(['something', 'bad'], $entity->errors('bar'));
+            $this->assertEquals(['something', 'bad'], $entity->getErrors('bar'));
 
             $expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
-            $this->assertEquals($expected, $entity->errors());
+            $this->assertEquals($expected, $entity->getErrors());
 
             $errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
-            $this->assertSame($entity, $entity->errors($errors, null, true));
+            $this->assertSame($entity, $entity->getErrors($errors, null, true));
             $errors['bar'] = ['else'];
-            $this->assertEquals($errors, $entity->errors());
+            $this->assertEquals($errors, $entity->getErrors());
         });
     }