Skip to content

Commit

Permalink
Fixes yiisoft#6618: Added Model::addErrors()
Browse files Browse the repository at this point in the history
  • Loading branch information
pana1990 authored and samdark committed Dec 26, 2014
1 parent e91b252 commit 7aae667
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Yii Framework 2 Change Log
- Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv)
- Enh #6467: `ActiveForm` will scroll to the nearest visible element when the first error input is hidden (newartix)
- Enh #6488: Support changing `yii\base\Theme::basePath` during runtime (qiangxue)
- Enh #6618: Added Model::addErrors() (slavcodev, pana1990)
- Chg #6427: In case of invalid route web application now throws exception with "Page not found" instead of "Invalid Route" (cebe, samdark)
- Chg #6641: removed zero padding from ETag strings (DaSourcerer)

Expand Down
21 changes: 21 additions & 0 deletions framework/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,27 @@ public function addError($attribute, $error = '')
$this->_errors[$attribute][] = $error;
}

/**
* Adds a list of errors.
* @param array $items a list of errors. The array keys must be attribute names.
* The array values should be error messages. If an attribute has multiple errors,
* these errors must be given in terms of an array.
* You may use the result of [[getErrors()]] as the value for this parameter.
* @since 2.0.2
*/
public function addErrors(array $items)
{
foreach ($items as $attribute => $errors) {
if (is_array($errors)) {
foreach($errors as $error) {
$this->addError($attribute, $error);
}
} else {
$this->addError($attribute, $errors);
}
}
}

/**
* Removes errors for all attributes or a single attribute.
* @param string $attribute attribute name. Use null to remove errors for all attribute.
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/framework/base/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,48 @@ public function testErrors()
$this->assertFalse($speaker->hasErrors());
}

public function testAddErrors()
{
$singer = new Singer();

$errors = ['firstName' => ['Something is wrong!']];
$singer->addErrors($errors);
$this->assertEquals($singer->getErrors(), $errors);

$singer->clearErrors();
$singer->addErrors(['firstName' => 'Something is wrong!']);
$this->assertEquals($singer->getErrors(), ['firstName' => ['Something is wrong!']]);

$singer->clearErrors();
$errors = ['firstName' => ['Something is wrong!', 'Totally wrong!']];
$singer->addErrors($errors);
$this->assertEquals($singer->getErrors(), $errors);

$singer->clearErrors();
$errors = [
'firstName' => ['Something is wrong!'],
'lastName' => ['Another one!']
];
$singer->addErrors($errors);
$this->assertEquals($singer->getErrors(), $errors);

$singer->clearErrors();
$errors = [
'firstName' => ['Something is wrong!', 'Totally wrong!'],
'lastName' => ['Another one!']
];
$singer->addErrors($errors);
$this->assertEquals($singer->getErrors(), $errors);

$singer->clearErrors();
$errors = [
'firstName' => ['Something is wrong!', 'Totally wrong!'],
'lastName' => ['Another one!', 'Totally wrong!']
];
$singer->addErrors($errors);
$this->assertEquals($singer->getErrors(), $errors);
}

public function testArraySyntax()
{
$speaker = new Speaker();
Expand Down

0 comments on commit 7aae667

Please sign in to comment.