facut pana la video 7
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class Button{
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<button type="submit" class="button">Submit</button>
|
||||
',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Field{
|
||||
public const TYPE_TEXT = 'text';
|
||||
public const TYPE_PASSWORD = 'password';
|
||||
|
||||
public string $type;
|
||||
public Model $model;
|
||||
public string $attribute;
|
||||
|
||||
public function __construct(Model $model, string $attribute){
|
||||
$this->type = self::TYPE_TEXT;
|
||||
$this->model = $model;
|
||||
$this->attribute = $attribute;
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<div class="mb-3">
|
||||
<label>%s</label>
|
||||
<input type="%s" name="%s" value="%s" class="form-control%s">
|
||||
<div class="invalid-feedback">
|
||||
%s
|
||||
</div>
|
||||
</div>
|
||||
',
|
||||
$this->model->labels()[$this->attribute] ?? $this->attribute,
|
||||
$this->type,
|
||||
$this->attribute,
|
||||
$this->model->{$this->attribute},
|
||||
$this->model->hasError($this->attribute) ? ' is invalid' : '',
|
||||
$this->model->getFirstError($this->attribute)
|
||||
);
|
||||
}
|
||||
|
||||
public function passwordField(){
|
||||
$this->type = self::TYPE_PASSWORD;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Form{
|
||||
public static function begin($action, $method){
|
||||
echo sprintf('<form action ="%s" method ="%s">', $action, $method);
|
||||
return new Form();
|
||||
}
|
||||
|
||||
public static function end(){
|
||||
echo '</form>';
|
||||
}
|
||||
|
||||
public function field(Model $model, $attribute){
|
||||
return new Field($model, $attribute);
|
||||
}
|
||||
|
||||
public function button(){
|
||||
return new Button();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user