Skip to content

Commit

Permalink
Allow specification of default attribute names
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Aug 29, 2018
1 parent 1208229 commit 574031d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ trait Validation
*/
protected $validationErrors;

/**
* @var array Default custom attribute names.
*/
protected $validationDefaultAttrNames = [];

/**
* Boot the validation trait for this model.
*
Expand Down Expand Up @@ -76,6 +81,28 @@ public static function bootValidation()
});
}

/**
* Programatically sets multiple validation attribute names.
* @param array $attributeNames
* @return void
*/
public function setValidationAttributeNames($attributeNames)
{
$this->validationDefaultAttrNames = $attributeNames;
}

/**
* Programatically sets the validation attribute names, will take lower priority
* to model defined attribute names found in `$attributeNames`.
* @param string $attr
* @param string $name
* @return void
*/
public function setValidationAttributeName($attr, $name)
{
$this->validationDefaultAttrNames[$attr] = $name;
}

/**
* Returns the model data used for validation.
* @return array
Expand Down Expand Up @@ -247,6 +274,8 @@ public function validate($rules = null, $customMessages = null, $attributeNames
$attributeNames = [];
}

$attributeNames = array_merge($this->validationDefaultAttrNames, $attributeNames);

if (property_exists($this, 'attributeNames')) {
$attributeNames = array_merge($this->attributeNames, $attributeNames);
}
Expand Down

0 comments on commit 574031d

Please sign in to comment.