Skip to content

Commit 40837c4

Browse files
authored
Merge pull request #25 from Schoolrunner/feature/humanized_attribute_key
Allow turning humanized keys on/off.
2 parents 218c804 + 3cc5435 commit 40837c4

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Attribute.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Attribute
99

1010
protected $key;
1111

12+
protected $humanizedKey;
13+
1214
protected $alias;
1315

1416
protected $validation;
@@ -24,6 +26,7 @@ public function __construct(Validation $validation, $key, $alias = null, array $
2426
$this->validation = $validation;
2527
$this->alias = $alias;
2628
$this->key = $key;
29+
$this->humanizedKey = ucfirst(str_replace('_', ' ', $key));
2730
foreach($rules as $rule) {
2831
$this->addRule($rule);
2932
}
@@ -94,6 +97,11 @@ public function getKey()
9497
return $this->key;
9598
}
9699

100+
public function getHumanizedKey()
101+
{
102+
return $this->humanizedKey;
103+
}
104+
97105
public function setAlias($alias)
98106
{
99107
$this->alias = $alias;

src/Validation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,10 @@ protected function resolveAttributeName(Attribute $attribute)
245245
return $this->aliases[$attribute->getKey()];
246246
} elseif($primaryAttribute AND isset($this->aliases[$primaryAttribute->getKey()])) {
247247
return $this->aliases[$primaryAttribute->getKey()];
248+
} elseif ($this->validator->getUseHumanizedKeys()) {
249+
return $attribute->getHumanizedKey();
248250
} else {
249-
return ucfirst(str_replace('_', ' ', $attribute->getKey()));
251+
return $attribute->getKey();
250252
}
251253
}
252254

src/Validator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Validator
1111

1212
protected $allowRuleOverride = false;
1313

14+
protected $useHumanizedKeys = true;
15+
1416
public function __construct(array $messages = [])
1517
{
1618
$this->messages = $messages;
@@ -124,4 +126,14 @@ public function allowRuleOverride($status = false)
124126
{
125127
$this->allowRuleOverride = $status;
126128
}
129+
130+
public function setUseHumanizedKeys($useHumanizedKeys = true)
131+
{
132+
$this->useHumanizedKeys = $useHumanizedKeys;
133+
}
134+
135+
public function getUseHumanizedKeys()
136+
{
137+
return $this->useHumanizedKeys;
138+
}
127139
}

0 commit comments

Comments
 (0)