diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index de7ad9cf16f..635aa6fbd98 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/base/Model.php b/framework/base/Model.php index 1d67c3f674b..1a795138261 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -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. diff --git a/tests/unit/framework/base/ModelTest.php b/tests/unit/framework/base/ModelTest.php index 6a2cd2aabe4..16953e99ebb 100644 --- a/tests/unit/framework/base/ModelTest.php +++ b/tests/unit/framework/base/ModelTest.php @@ -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();