facut pana la video 7

This commit is contained in:
Cerbu Andrei Mihnea
2023-05-10 00:47:48 +03:00
parent 1abdc313f4
commit 90f12ab916
40 changed files with 1644 additions and 41 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace app\core\form;
class Button{
public function __toString(){
return sprintf('
<button type="submit" class="button">Submit</button>
',
);
}
}
+43
View File
@@ -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;
}
}
+23
View File
@@ -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();
}
}