microserviciile de register, login finalizate
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
<?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)
|
||||
);
|
||||
}
|
||||
}
|
||||
Regular → Executable
+11
-11
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class Button{
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<button type="submit" class="button">Submit</button>
|
||||
',
|
||||
);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class Button{
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<button type="submit" class="button">Submit</button>
|
||||
',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Regular → Executable
+30
-22
@@ -1,23 +1,31 @@
|
||||
<?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 inputField(Model $model, $attribute){
|
||||
return new InputField($model, $attribute);
|
||||
}
|
||||
|
||||
public function button(){
|
||||
return new Button();
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Form{
|
||||
private string $formDestinationPage = "";
|
||||
|
||||
public function __construct(string $formDestinationPage){
|
||||
$this->formDestinationPage = $formDestinationPage;
|
||||
}
|
||||
public function beginForm(): void
|
||||
{
|
||||
echo "<form action='$this->formDestinationPage' method ='POST'>";
|
||||
}
|
||||
|
||||
public function endForm(): void
|
||||
{
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
public function renderInputField(string $type, string $name, string $placeholder, string $value, ?string $error): void
|
||||
{
|
||||
echo new InputField($type, $name, $placeholder, $value, $error);
|
||||
}
|
||||
|
||||
public function renderButton(): void
|
||||
{
|
||||
echo new Button();
|
||||
}
|
||||
}
|
||||
Regular → Executable
+41
-35
@@ -1,36 +1,42 @@
|
||||
<?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' : ''
|
||||
);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class InputField
|
||||
{
|
||||
private string $type;
|
||||
private string $name;
|
||||
private string $placeholder;
|
||||
private string $value;
|
||||
private ?string $error;
|
||||
|
||||
public function __construct(string $type, string $name, string $placeholder, string $value, ?string $error)
|
||||
{
|
||||
$this->type = $this->getInputType($type);
|
||||
$this->name = $name;
|
||||
$this->placeholder = $placeholder;
|
||||
$this->value = $value;
|
||||
$this->error = $error;
|
||||
}
|
||||
public function __toString():string
|
||||
{
|
||||
$field = sprintf(
|
||||
'<input type="%s" name="%s" placeholder="%s" value="%s">',
|
||||
$this->type, $this->name, $this->placeholder, $this->value
|
||||
);
|
||||
|
||||
$errorField = NULL;
|
||||
if(!$this->error == NULL){
|
||||
$errorField = sprintf('<p class="errorStyle">%s</p>', $this->error);
|
||||
}
|
||||
|
||||
return $field.$errorField;
|
||||
}
|
||||
|
||||
private function getInputType(string $type): string{
|
||||
return match($type){
|
||||
"username", "email" => "text",
|
||||
"password" => "password"
|
||||
};
|
||||
}
|
||||
}
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
use app\core\Model;
|
||||
|
||||
class Region extends BaseField
|
||||
{
|
||||
public const TYPE_TEXT = 'region';
|
||||
public string $type;
|
||||
|
||||
public function __construct(Model $model, string $attribute)
|
||||
{
|
||||
$this->type = self::TYPE_TEXT;
|
||||
parent::__construct($model, $attribute);
|
||||
}
|
||||
|
||||
public function startSelect(): string
|
||||
{
|
||||
return sprintf('<select class="%s" name="%s">',
|
||||
$this->type,
|
||||
$this->type,
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function endSelect(): string
|
||||
{
|
||||
return '</select>';
|
||||
}
|
||||
|
||||
public function renderInput(): string
|
||||
{
|
||||
return $this->startSelect().$this->content().$this->endSelect();
|
||||
}
|
||||
}
|
||||
Regular → Executable
+12
-12
@@ -1,13 +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}
|
||||
);
|
||||
}
|
||||
<?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