/** * Returns all the error messages as an array, including error key. * * <code> * $model->errors->errors(); * * # array( * # "name" => array("Name can't be blank"), * # "state" => array("State is the wrong length (should be 2 chars)") * # ) * </code> * * @param callable $closure Closure to fetch the errors in some other format (optional) * This closure has the signature function($attribute, $message) * and is called for each available error message. * @return array */ public function to_array($closure = null) { $errors = array(); if ($this->errors) { foreach ($this->errors as $attribute => $messages) { foreach ($messages as $msg) { if (is_null($msg)) { continue; } $errors[$attribute][] = $message = Utils::human_attribute($attribute) . ' ' . $msg; if ($closure) { $closure($attribute, $message); } } } } return $errors; }
protected function _translate_error($attribute, $error) { if ($this->gettext_available) { $attribute = gettext(Utils::human_attribute($attribute)); $error = gettext($error); } else { $attribute = Utils::human_attribute($attribute); } return sprintf($error, $attribute); }
/** * Returns all the error messages as an array. * * <code> * $model->errors->full_messages(); * * # array( * # "Name can't be blank", * # "State is the wrong length (should be 2 chars)" * # ) * </code> * * @param array $options Options for messages * @return array */ public function full_messages() { $full_messages = array(); if ($this->errors) { foreach ($this->errors as $attribute => $messages) { foreach ($messages as $msg) { if (is_null($msg)) { continue; } $full_messages[] = Utils::human_attribute($attribute) . ' ' . $msg; } } } return $full_messages; }
/** * Add an error message. * * @param string $attribute Name of an attribute on the model * @param string $msg The error message */ public function add($attribute, $msg) { $model = $this->modelName; if (is_null($msg)) { $msg = self::$DEFAULT_ERROR_MESSAGES['invalid']; } if (isset($model::$attr_names[$attribute])) { $msg = $model::$attr_names[$attribute] . ' ' . $msg; } else { $msg = Utils::human_attribute($attribute) . ' ' . $msg; } if (!isset($this->errors[$attribute])) { $this->errors[$attribute] = array($msg); } else { $this->errors[$attribute][] = $msg; } }