Skip to content

Commit e80b948

Browse files
committed
Create getLabel method in Model.php and fix bug in Field.php if label does not exist for attribute
1 parent e9e01dd commit e80b948

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

core/Model.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function labels()
4444
return [];
4545
}
4646

47+
public function getLabel($attribute)
48+
{
49+
return $this->labels()[$attribute] ?? $attribute;
50+
}
51+
4752
public function rules()
4853
{
4954
return [];
@@ -59,19 +64,19 @@ public function validate()
5964
$ruleName = $rule[0];
6065
}
6166
if ($ruleName === self::RULE_REQUIRED && !$value) {
62-
$this->addError($attribute, self::RULE_REQUIRED);
67+
$this->addErrorByRule($attribute, self::RULE_REQUIRED);
6368
}
6469
if ($ruleName === self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
65-
$this->addError($attribute, self::RULE_EMAIL);
70+
$this->addErrorByRule($attribute, self::RULE_EMAIL);
6671
}
6772
if ($ruleName === self::RULE_MIN && strlen($value) < $rule['min']) {
68-
$this->addError($attribute, self::RULE_MIN, ['min' => $rule['min']]);
73+
$this->addErrorByRule($attribute, self::RULE_MIN, ['min' => $rule['min']]);
6974
}
7075
if ($ruleName === self::RULE_MAX && strlen($value) > $rule['max']) {
71-
$this->addError($attribute, self::RULE_MAX);
76+
$this->addErrorByRule($attribute, self::RULE_MAX);
7277
}
7378
if ($ruleName === self::RULE_MATCH && $value !== $this->{$rule['match']}) {
74-
$this->addError($attribute, self::RULE_MATCH, ['match' => $rule['match']]);
79+
$this->addErrorByRule($attribute, self::RULE_MATCH, ['match' => $rule['match']]);
7580
}
7681
if ($ruleName === self::RULE_UNIQUE) {
7782
$className = $rule['class'];
@@ -83,7 +88,7 @@ public function validate()
8388
$statement->execute();
8489
$record = $statement->fetchObject();
8590
if ($record) {
86-
$this->addError($attribute, self::RULE_UNIQUE);
91+
$this->addErrorByRule($attribute, self::RULE_UNIQUE);
8792
}
8893
}
8994
}
@@ -108,7 +113,7 @@ public function errorMessage($rule)
108113
return $this->errorMessages()[$rule];
109114
}
110115

111-
public function addError(string $attribute, string $rule, $params = [])
116+
protected function addErrorByRule(string $attribute, string $rule, $params = [])
112117
{
113118
$params['field'] ??= $attribute;
114119
$errorMessage = $this->errorMessage($rule);
@@ -118,6 +123,11 @@ public function addError(string $attribute, string $rule, $params = [])
118123
$this->errors[$attribute][] = $errorMessage;
119124
}
120125

126+
public function addError(string $attribute, string $message)
127+
{
128+
$this->errors[$attribute][] = $message;
129+
}
130+
121131
public function hasError($attribute)
122132
{
123133
return $this->errors[$attribute] ?? false;

core/form/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __toString()
4747
%s
4848
</div>
4949
</div>',
50-
$this->model->labels()[$this->attribute],
50+
$this->model->getLabel($this->attribute),
5151
$this->type,
5252
$this->model->hasError($this->attribute) ? ' is-invalid' : '',
5353
$this->attribute,

0 commit comments

Comments
 (0)