@@ -194,7 +194,8 @@ public function getAttributes($names = null, $except = array()) {
194194
195195 /**
196196 * Saves the parent model and the current model.
197- * DO NOT OVERRIDE THIS METHOD or functionality of this trait will be lost.
197+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
198+ * trait will be lost.
198199 *
199200 * @return boolean
200201 * @throws Exception
@@ -225,7 +226,8 @@ public function save($runValidation = true, $attributeNames = null) {
225226
226227 /**
227228 * Validates the parent and the current model.
228- * DO NOT OVERRIDE THIS METHOD or functionality of this trait will be lost.
229+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
230+ * trait will be lost.
229231 *
230232 * @return boolean
231233 * @throws Exception
@@ -237,6 +239,89 @@ public function validate($attributeNames = null, $clearErrors = true) {
237239 return $ this ->_parent ()->validate ($ attributeNames , $ clearErrors ) && $ r ;
238240 }
239241
242+ /**
243+ * Returns a value indicating whether there is any validation error.
244+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
245+ * trait will be lost.
246+ *
247+ * @param string|null $attribute attribute name. Use null to check all attributes.
248+ * @return boolean whether there is any error.
249+ */
250+ public function hasErrors ($ attribute = null ) {
251+ return $ this ->_parent ()->hasErrors ($ attribute ) || parent ::hasErrors ($ attribute );
252+ }
253+
254+ /**
255+ * Returns the errors for all attribute or a single attribute.
256+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
257+ * trait will be lost.
258+ *
259+ * @param string $attribute attribute name. Use null to retrieve errors for all attributes.
260+ * @property array An array of errors for all attributes. Empty array is returned if no error.
261+ * The result is a two-dimensional array. See [[getErrors()]] for detailed description.
262+ * @return array errors for all attributes or the specified attribute. Empty array is returned if no error.
263+ * Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
264+ *
265+ * ~~~
266+ * [
267+ * 'username' => [
268+ * 'Username is required.',
269+ * 'Username must contain only word characters.',
270+ * ],
271+ * 'email' => [
272+ * 'Email address is invalid.',
273+ * ]
274+ * ]
275+ * ~~~
276+ *
277+ * @see getFirstErrors()
278+ * @see getFirstError()
279+ */
280+ public function getErrors ($ attribute = null ) {
281+ return array_merge ($ this ->_parent ()->getErrors ($ attribute ), parent ::getErrors ($ attribute ));
282+ }
283+
284+ /**
285+ * Returns the first error of every attribute in the model.
286+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
287+ * trait will be lost.
288+ *
289+ * @return array the first errors. The array keys are the attribute names, and the array
290+ * values are the corresponding error messages. An empty array will be returned if there is no error.
291+ * @see getErrors()
292+ * @see getFirstError()
293+ */
294+ public function getFirstErrors () {
295+ $ errs = $ this ->getErrors ();
296+ if (empty ($ errs )) {
297+ return [];
298+ } else {
299+ $ errors = [];
300+ foreach ($ errs as $ name => $ es ) {
301+ if (!empty ($ es )) {
302+ $ errors [$ name ] = reset ($ es );
303+ }
304+ }
305+
306+ return $ errors ;
307+ }
308+ }
309+
310+ /**
311+ * Returns the first error of the specified attribute.
312+ * DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
313+ * trait will be lost.
314+ *
315+ * @param string $attribute attribute name.
316+ * @return string the error message. Null is returned if no error.
317+ * @see getErrors()
318+ * @see getFirstErrors()
319+ */
320+ public function getFirstError ($ attribute ) {
321+ $ errors = $ this ->getErrors ($ attribute );
322+ return empty ($ errors [$ attribute ]) ? null : $ errors [0 ];
323+ }
324+
240325 /**
241326 * @inheritdoc
242327 */
@@ -274,4 +359,5 @@ public function parentPrimaryKey() {
274359 $ pClass = static ::extendsFrom ();
275360 return $ pClass ::primaryKey ()[0 ];
276361 }
362+
277363}
0 commit comments