Skip to content

Commit

Permalink
Add Form::setErrors().
Browse files Browse the repository at this point in the history
Closes #10971
  • Loading branch information
ADmad committed Aug 27, 2017
1 parent 7c98803 commit 37c1f1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Form/Form.php
Expand Up @@ -158,6 +158,28 @@ public function errors()
return $this->_errors;
}

/**
* 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.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/Form/FormTest.php
Expand Up @@ -115,6 +115,22 @@ public function testErrors()
$this->assertEquals('Must be so long', $errors['body']['length']);
}

/**
* 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.
*
Expand Down

0 comments on commit 37c1f1b

Please sign in to comment.