gata basic ul

This commit is contained in:
Cerbu Andrei Mihnea
2023-05-10 03:28:30 +03:00
parent 90f12ab916
commit a739a66ae4
19 changed files with 255 additions and 110 deletions
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace app\core\form;
use app\core\Model;
abstract class BaseField
{
public Model $model;
public string $attribute;
public function __construct(Model $model, string $attribute)
{
$this->model = $model;
$this->attribute = $attribute;
}
abstract public function renderInput(): string;
public function __toString()
{
return sprintf('
<div class="mb-3">
<label>%s</label>
%s
<div class="invalid-feedback">
%s
</div>
</div>
',
$this->model->labels()[$this->attribute] ?? $this->attribute,
$this->renderInput(),
$this->model->getFirstError($this->attribute)
);
}
}
+2 -2
View File
@@ -13,8 +13,8 @@ class Form{
echo '</form>';
}
public function field(Model $model, $attribute){
return new Field($model, $attribute);
public function inputField(Model $model, $attribute){
return new InputField($model, $attribute);
}
public function button(){
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace app\core\form;
use app\core\Model;
class InputField extends BaseField
{
public const TYPE_TEXT = 'text';
public const TYPE_PASSWORD = 'password';
public string $type;
public function __construct(Model $model, string $attribute)
{
$this->type = self::TYPE_TEXT;
parent::__construct($model, $attribute);
}
public function passwordField()
{
$this->type = self::TYPE_PASSWORD;
return $this;
}
public function renderInput(): string
{
return sprintf(
'<input type="%s" name="%s" value="%s" class="form-control%s">',
$this->type,
$this->attribute,
$this->model->{$this->attribute},
$this->model->hasError($this->attribute) ? ' is invalid' : ''
);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace app\core\form;
class TextareaField extends BaseField{
public function renderInput(): string{
return sprintf('<textarea name=%s class=form-control%s>%s</textarea>',
$this->attribute,
$this->model->hasError($this->attribute) ? ' is invalid' : '',
$this->model->{$this->attribute}
);
}
}