From 98886f12ee659ace20b72cc1077c07eac0acdd61 Mon Sep 17 00:00:00 2001 From: Jarbit Lira Date: Sun, 26 Jul 2015 13:50:05 -0600 Subject: [PATCH] add custom attributes names for error messages --- src/LaravelBook/Ardent/Ardent.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index ba6f026..2474687 100755 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -45,6 +45,13 @@ abstract class Ardent extends Model { */ public static $customMessages = array(); + /** + * The array of custom attributes names in error messages + * + * @var array + */ + public static $niceNames = array(); + /** * The validator object in case you need it externally (say, for a form builder). * @@ -529,10 +536,11 @@ protected static function makeValidator($data, $rules, $customMessages) { * * @param array $rules Validation rules * @param array $customMessages Custom error messages + * @param array $niceNames Custom attributes names on error messages * @return bool * @throws InvalidModelException */ - public function validate(array $rules = array(), array $customMessages = array()) { + public function validate(array $rules = array(), array $customMessages = array(), array $niceNames = array()) { if ($this->fireModelEvent('validating') === false) { if ($this->throwOnValidation) { throw new InvalidModelException($this); @@ -562,7 +570,14 @@ public function validate(array $rules = array(), array $customMessages = array() // perform validation $this->validator = static::makeValidator($data, $rules, $customMessages); - $success = $this->validator->passes(); + + //set custom attribute names + if (method_exists($this->validator, 'setAttributeNames')) { + $niceNames = (empty($niceNames))? static::$niceNames : $niceNames; + $this->validator->setAttributeNames($niceNames); + } + + $success = $this->validator->passes(); if ($success) { // if the model is valid, unset old errors