From e681272d3fa4df24e4c404a43c83831cb554ff42 Mon Sep 17 00:00:00 2001 From: --replace-all Date: Thu, 9 Apr 2015 17:28:17 +0200 Subject: [PATCH] Added errors functionality to the trait. Now child classes gets errors generated on parent class --- src/ActiveRecordInheritanceTrait.php | 90 +++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecordInheritanceTrait.php b/src/ActiveRecordInheritanceTrait.php index 933dc0d..d3580ce 100644 --- a/src/ActiveRecordInheritanceTrait.php +++ b/src/ActiveRecordInheritanceTrait.php @@ -194,7 +194,8 @@ public function getAttributes($names = null, $except = array()) { /** * Saves the parent model and the current model. - * DO NOT OVERRIDE THIS METHOD or functionality of this trait will be lost. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. * * @return boolean * @throws Exception @@ -225,7 +226,8 @@ public function save($runValidation = true, $attributeNames = null) { /** * Validates the parent and the current model. - * DO NOT OVERRIDE THIS METHOD or functionality of this trait will be lost. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. * * @return boolean * @throws Exception @@ -237,6 +239,89 @@ public function validate($attributeNames = null, $clearErrors = true) { return $this->_parent()->validate($attributeNames, $clearErrors) && $r; } + /** + * Returns a value indicating whether there is any validation error. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. + * + * @param string|null $attribute attribute name. Use null to check all attributes. + * @return boolean whether there is any error. + */ + public function hasErrors($attribute = null) { + return $this->_parent()->hasErrors($attribute) || parent::hasErrors($attribute); + } + + /** + * Returns the errors for all attribute or a single attribute. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. + * + * @param string $attribute attribute name. Use null to retrieve errors for all attributes. + * @property array An array of errors for all attributes. Empty array is returned if no error. + * The result is a two-dimensional array. See [[getErrors()]] for detailed description. + * @return array errors for all attributes or the specified attribute. Empty array is returned if no error. + * Note that when returning errors for all attributes, the result is a two-dimensional array, like the following: + * + * ~~~ + * [ + * 'username' => [ + * 'Username is required.', + * 'Username must contain only word characters.', + * ], + * 'email' => [ + * 'Email address is invalid.', + * ] + * ] + * ~~~ + * + * @see getFirstErrors() + * @see getFirstError() + */ + public function getErrors($attribute = null) { + return array_merge($this->_parent()->getErrors($attribute), parent::getErrors($attribute)); + } + + /** + * Returns the first error of every attribute in the model. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. + * + * @return array the first errors. The array keys are the attribute names, and the array + * values are the corresponding error messages. An empty array will be returned if there is no error. + * @see getErrors() + * @see getFirstError() + */ + public function getFirstErrors() { + $errs = $this->getErrors(); + if (empty($errs)) { + return []; + } else { + $errors = []; + foreach ($errs as $name => $es) { + if (!empty($es)) { + $errors[$name] = reset($es); + } + } + + return $errors; + } + } + + /** + * Returns the first error of the specified attribute. + * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this + * trait will be lost. + * + * @param string $attribute attribute name. + * @return string the error message. Null is returned if no error. + * @see getErrors() + * @see getFirstErrors() + */ + public function getFirstError($attribute) { + $errors = $this->getErrors($attribute); + return empty($errors[$attribute]) ? null : $errors[0]; + } + /** * @inheritdoc */ @@ -274,4 +359,5 @@ public function parentPrimaryKey() { $pClass = static::extendsFrom(); return $pClass::primaryKey()[0]; } + }