From fd386c949c726e31b60e68ea45f87ec88d9b66aa Mon Sep 17 00:00:00 2001 From: SilverFire - Dima Naumenko Date: Wed, 10 Jun 2015 20:32:41 +0000 Subject: [PATCH] Fixed validate() - fail if one of models failed to validate --- Collection.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Collection.php b/Collection.php index 811c63d..153e6a6 100644 --- a/Collection.php +++ b/Collection.php @@ -366,12 +366,32 @@ public function hasErrors () { return false; } + /** + * Returns the first error of the collection + * + * @return bool|mixed + */ + public function getFirstError () { + foreach ($this->models as $model) { + /* @var $model ActiveRecord */ + if ($model->hasErrors()) { + $errors = $model->getFirstErrors(); + return array_shift($errors); + } + } + + return false; + } + + public function validate ($attributes = null) { if (!$this->beforeValidate()) { return false; } - $this->first->validateMultiple($this->models, $attributes); + if (!$this->first->validateMultiple($this->models, $attributes)) { + return false; + } $this->afterValidate();