Browse Source

Add Form::setErrors().

Closes #10971
ADmad 8 years ago
parent
commit
37c1f1b24d
2 changed files with 38 additions and 0 deletions
  1. 22 0
      src/Form/Form.php
  2. 16 0
      tests/TestCase/Form/FormTest.php

+ 22 - 0
src/Form/Form.php

@@ -159,6 +159,28 @@ class Form
     }
 
     /**
+     * Set the errors in the form.
+     *
+     * ```
+     * $errors = [
+     *      'field_name' => ['rule_name' => 'message']
+     * ];
+     *
+     * $form->setErrors($errors);
+     * ```
+     *
+     * @since 3.5.1
+     * @param array $errors Errors list.
+     * @return $this
+     */
+    public function setErrors(array $errors)
+    {
+        $this->_errors = $errors;
+
+        return $this;
+    }
+
+    /**
      * Execute the form if it is valid.
      *
      * First validates the form, then calls the `_execute()` hook method.

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

@@ -116,6 +116,22 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test setErrors()
+     *
+     * @return void
+     */
+    public function testSetErrors()
+    {
+        $form = new Form();
+        $expected = [
+           'field_name' => ['rule_name' => 'message']
+        ];
+
+        $form->setErrors($expected);
+        $this->assertSame($expected, $form->errors());
+    }
+
+    /**
      * Test _execute is skipped on validation failure.
      *
      * @return void