Commit 7aae667e by pana1990 Committed by Alexander Makarov

Fixes #6618: Added Model::addErrors()

parent e91b252b
......@@ -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)
......
......@@ -575,6 +575,27 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab
}
/**
* 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.
*/
......
......@@ -170,6 +170,48 @@ class ModelTest extends TestCase
$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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment