gata basic ul
This commit is contained in:
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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(){
|
||||
|
||||
@@ -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' : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user