Skip to content

Commit 45355f0

Browse files
committed
Implement attribute labels on model and display them in form
1 parent d265df6 commit 45355f0

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

core/DbModel.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ abstract class DbModel extends Model
1818
{
1919
abstract public static function tableName(): string;
2020

21-
abstract public function attributes(): array;
22-
2321
public function primaryKey(): string
2422
{
2523
return 'id';

core/Model.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public function loadData($data)
3434
}
3535
}
3636

37+
public function attributes()
38+
{
39+
return [];
40+
}
41+
42+
public function labels()
43+
{
44+
return [];
45+
}
46+
3747
public function rules()
3848
{
3949
return [];

core/form/Field.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Field
3131
* @param \app\core\Model $model
3232
* @param string $attribute
3333
*/
34-
public function __construct(\app\core\Model $model, string $attribute)
34+
public function __construct(Model $model, string $attribute)
3535
{
3636
$this->type = self::TYPE_TEXT;
3737
$this->model = $model;
@@ -47,7 +47,7 @@ public function __toString()
4747
%s
4848
</div>
4949
</div>',
50-
$this->attribute,
50+
$this->model->labels()[$this->attribute],
5151
$this->type,
5252
$this->model->hasError($this->attribute) ? ' is-invalid' : '',
5353
$this->attribute,

models/User.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010

1111
use app\core\DbModel;
12-
use app\core\Model;
1312

1413
/**
1514
* Class Register
@@ -35,6 +34,17 @@ public function attributes(): array
3534
return ['firstname', 'lastname', 'email', 'password'];
3635
}
3736

37+
public function labels(): array
38+
{
39+
return [
40+
'firstname' => 'First name',
41+
'lastname' => 'Last name',
42+
'email' => 'Email',
43+
'password' => 'Password',
44+
'passwordConfirm' => 'Password Confirm'
45+
];
46+
}
47+
3848
public function rules()
3949
{
4050
return [

0 commit comments

Comments
 (0)