Skip to content

Commit ee0bd65

Browse files
committed
Create BaseField and TextareaField.php
1 parent dadd3fb commit ee0bd65

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

core/form/BaseField.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 7/26/2020
5+
* Time: 3:49 PM
6+
*/
7+
8+
namespace app\core\form;
9+
10+
11+
use app\core\Model;
12+
13+
/**
14+
* Class BaseField
15+
*
16+
* @author Zura Sekhniashvili <zurasekhniashvili@gmail.com>
17+
* @package app\core\form
18+
*/
19+
abstract class BaseField
20+
{
21+
22+
public Model $model;
23+
public string $attribute;
24+
public string $type;
25+
26+
/**
27+
* Field constructor.
28+
*
29+
* @param \app\core\Model $model
30+
* @param string $attribute
31+
*/
32+
public function __construct(Model $model, string $attribute)
33+
{
34+
$this->model = $model;
35+
$this->attribute = $attribute;
36+
}
37+
38+
public function __toString()
39+
{
40+
return sprintf('<div class="form-group">
41+
<label>%s</label>
42+
%s
43+
<div class="invalid-feedback">
44+
%s
45+
</div>
46+
</div>',
47+
$this->model->getLabel($this->attribute),
48+
$this->renderInput(),
49+
$this->model->getFirstError($this->attribute)
50+
);
51+
}
52+
53+
abstract public function renderInput();
54+
}

core/form/Field.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
* @author Zura Sekhniashvili <zurasekhniashvili@gmail.com>
1717
* @package core\form
1818
*/
19-
class Field
19+
class Field extends BaseField
2020
{
2121
const TYPE_TEXT = 'text';
2222
const TYPE_PASSWORD = 'password';
23-
24-
public Model $model;
25-
public string $attribute;
26-
public string $type;
23+
const TYPE_FILE = 'file';
2724

2825
/**
2926
* Field constructor.
@@ -34,25 +31,16 @@ class Field
3431
public function __construct(Model $model, string $attribute)
3532
{
3633
$this->type = self::TYPE_TEXT;
37-
$this->model = $model;
38-
$this->attribute = $attribute;
34+
parent::__construct($model, $attribute);
3935
}
4036

41-
public function __toString()
37+
public function renderInput()
4238
{
43-
return sprintf('<div class="form-group">
44-
<label>%s</label>
45-
<input type="%s" class="form-control%s" name="%s" value="%s">
46-
<div class="invalid-feedback">
47-
%s
48-
</div>
49-
</div>',
50-
$this->model->getLabel($this->attribute),
39+
return sprintf('<input type="%s" class="form-control%s" name="%s" value="%s">',
5140
$this->type,
5241
$this->model->hasError($this->attribute) ? ' is-invalid' : '',
5342
$this->attribute,
5443
$this->model->{$this->attribute},
55-
$this->model->getFirstError($this->attribute)
5644
);
5745
}
5846

@@ -61,4 +49,10 @@ public function passwordField()
6149
$this->type = self::TYPE_PASSWORD;
6250
return $this;
6351
}
52+
53+
public function fileField()
54+
{
55+
$this->type = self::TYPE_FILE;
56+
return $this;
57+
}
6458
}

core/form/TextareaField.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 7/26/2020
5+
* Time: 3:49 PM
6+
*/
7+
8+
namespace app\core\form;
9+
10+
11+
/**
12+
* Class TextareaField
13+
*
14+
* @author Zura Sekhniashvili <zurasekhniashvili@gmail.com>
15+
* @package app\core\form
16+
*/
17+
class TextareaField extends BaseField
18+
{
19+
public function renderInput()
20+
{
21+
return sprintf('<textarea class="form-control%s" name="%s">%s</textarea>',
22+
$this->model->hasError($this->attribute) ? ' is-invalid' : '',
23+
$this->attribute,
24+
$this->model->{$this->attribute},
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)