Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #213 from ricardoriogo/master
Browse files Browse the repository at this point in the history
Added the possibility to define custom attributes's names on models
  • Loading branch information
igorsantos07 committed Sep 7, 2015
2 parents 51b99b4 + f649633 commit c655360
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ abstract class Ardent extends Model {
public static $customMessages = array();

/**
* The array of custom attributes.
*
* @var array
*/
public static $customAttributes = array();

/**
* The validator object in case you need it externally (say, for a form builder).
*
* @see getValidator()
Expand Down Expand Up @@ -513,26 +520,28 @@ public static function configureAsExternal(array $connection, $lang = 'en') {
* @param $data
* @param $rules
* @param $customMessages
* @param $customAttributes
* @return \Illuminate\Validation\Validator
* @see Ardent::$externalValidator
*/
protected static function makeValidator($data, $rules, $customMessages) {
protected static function makeValidator($data, $rules, $customMessages, $customAttributes) {
if (self::$externalValidator) {
return self::$validationFactory->make($data, $rules, $customMessages);
return self::$validationFactory->make($data, $rules, $customMessages, $customAttributes);
} else {
return Validator::make($data, $rules, $customMessages);
return Validator::make($data, $rules, $customMessages, $customAttributes);
}
}

/**
* Validate the model instance
*
* @param array $rules Validation rules
* @param array $customMessages Custom error messages
* @param array $rules Validation rules
* @param array $customMessages Custom error messages
* @param array $customAttributes Custom attributes
* @return bool
* @throws InvalidModelException
*/
public function validate(array $rules = array(), array $customMessages = array()) {
public function validate(array $rules = array(), array $customMessages = array(), array $customAttributes = array()) {
if ($this->fireModelEvent('validating') === false) {
if ($this->throwOnValidation) {
throw new InvalidModelException($this);
Expand All @@ -553,6 +562,7 @@ public function validate(array $rules = array(), array $customMessages = array()
$success = true;
} else {
$customMessages = (empty($customMessages))? static::$customMessages : $customMessages;
$customAttributes = (empty($customAttributes))? static::$customAttributes : $customAttributes;

if ($this->forceEntityHydrationFromInput || (empty($this->attributes) && $this->autoHydrateEntityFromInput)) {
$this->fill(Input::all());
Expand All @@ -561,7 +571,7 @@ public function validate(array $rules = array(), array $customMessages = array()
$data = $this->getAttributes(); // the data under validation

// perform validation
$this->validator = static::makeValidator($data, $rules, $customMessages);
$this->validator = static::makeValidator($data, $rules, $customMessages, $customAttributes);
$success = $this->validator->passes();

if ($success) {
Expand Down

0 comments on commit c655360

Please sign in to comment.