update 1
This commit is contained in:
@ -3,19 +3,43 @@
|
||||
namespace itguild\forms;
|
||||
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
use itguild\forms\builders\TextAreaBuilder;
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\Select;
|
||||
use itguild\forms\inputs\TextArea;
|
||||
use itguild\forms\templates\Template;
|
||||
|
||||
class ActiveForm
|
||||
{
|
||||
private BaseInput $fieldObject;
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @return void
|
||||
*/
|
||||
public function beginForm(string $action): void
|
||||
{
|
||||
echo "<form method='POST' action='$action'>";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function endForm(): void
|
||||
{
|
||||
echo "</form>";
|
||||
|
||||
}
|
||||
|
||||
public function field($class, string $name, array $params = [])
|
||||
{
|
||||
if ($class === Select::class){
|
||||
$this->fieldObject = SelectBuilder::build($name, $params);
|
||||
}
|
||||
if ($class === TextArea::class){
|
||||
$this->fieldObject = TextAreaBuilder::build($name, $params);
|
||||
}
|
||||
else {
|
||||
$this->fieldObject = new $class($name, $params);
|
||||
}
|
||||
@ -37,6 +61,13 @@ class ActiveForm
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->fieldObject->setTemplate($template);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$this->fieldObject->create();
|
||||
|
@ -18,7 +18,7 @@ class Form {
|
||||
*/
|
||||
public function beginForm(string $action): void
|
||||
{
|
||||
echo "<form action='$action'>";
|
||||
echo "<form method='POST' action='$action'>";
|
||||
}
|
||||
|
||||
/**
|
||||
|
19
src/builders/TextAreaBuilder.php
Normal file
19
src/builders/TextAreaBuilder.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
|
||||
use itguild\forms\inputs\Select;
|
||||
use itguild\forms\inputs\TextArea;
|
||||
|
||||
class TextAreaBuilder
|
||||
{
|
||||
|
||||
public static function build(string $name, array $params = [])
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
|
||||
return new TextArea(name: $name, value: $value, paramsArray: $params);
|
||||
}
|
||||
|
||||
}
|
@ -3,9 +3,12 @@
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\templates\Template;
|
||||
use itguild\forms\traits\CreateOption;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
|
||||
|
||||
class Select extends BaseInput
|
||||
{
|
||||
@ -18,6 +21,7 @@ class Select extends BaseInput
|
||||
private string $labelTitle = '';
|
||||
private $value;
|
||||
private $paramsArray;
|
||||
private Template $selectTemplate;
|
||||
|
||||
|
||||
/**
|
||||
@ -32,6 +36,7 @@ class Select extends BaseInput
|
||||
$this->options = $options;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->selectTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,10 +46,13 @@ class Select extends BaseInput
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$optionsString = $this->createOption($this->options, $this->value);
|
||||
$this->html = "<select name='$this->name' $paramsString>$optionsString</select>";
|
||||
$label = "";
|
||||
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{select}', $select, $this->selectTemplate->getSelectTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
|
||||
@ -59,8 +67,8 @@ class Select extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
|
||||
{
|
||||
$textarea = new self($name, $options, $value, $paramsArray);
|
||||
$textarea->create()->render();
|
||||
$select = new self($name, $options, $value, $paramsArray);
|
||||
$select->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,4 +92,15 @@ class Select extends BaseInput
|
||||
$this->options = array_merge($options, $this->options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->selectTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -4,7 +4,9 @@ namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
|
||||
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\templates\Template;
|
||||
class TextArea extends BaseInput
|
||||
{
|
||||
use CreateParams;
|
||||
@ -14,7 +16,7 @@ class TextArea extends BaseInput
|
||||
private $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
private Template $textareaTemplate;
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
@ -25,6 +27,7 @@ class TextArea extends BaseInput
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->textareaTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,11 +36,15 @@ class TextArea extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<textarea name='$this->name' $paramsString>$this->value</textarea>";
|
||||
$label = "";
|
||||
$textarea = "<textarea name='$this->name' $paramsString>$this->value</textarea>";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{textarea}', $textarea, $this->textareaTemplate->getTextAreaTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -66,5 +73,16 @@ class TextArea extends BaseInput
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->textareaTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -2,7 +2,11 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\templates\Template;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
|
||||
class TextInput extends BaseInput
|
||||
@ -15,6 +19,8 @@ class TextInput extends BaseInput
|
||||
private string $labelTitle = '';
|
||||
private array $paramsArray;
|
||||
|
||||
private Template $inputTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
@ -24,6 +30,7 @@ class TextInput extends BaseInput
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -32,11 +39,15 @@ class TextInput extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<input name='$this->name' $paramsString >";
|
||||
$label = "";
|
||||
$input = "<input name='$this->name' $paramsString >";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -64,5 +75,16 @@ class TextInput extends BaseInput
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->inputTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
38
src/templates/Simple/SimpleTemplate.php
Normal file
38
src/templates/Simple/SimpleTemplate.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\templates\Simple;
|
||||
|
||||
use itguild\forms\templates\Template;
|
||||
|
||||
class SimpleTemplate extends Template
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getInputTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{input}</div>";
|
||||
}
|
||||
|
||||
public static function getTextAreaTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{textarea}</div>";
|
||||
}
|
||||
public static function getSelectTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{select}</div>";
|
||||
}
|
||||
public static function getRadioTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{radio}</div>";
|
||||
}
|
||||
public static function getCheckBoxTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{checkbox}</div>";
|
||||
}
|
||||
public static function getButtonTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{button}</div>";
|
||||
}
|
||||
}
|
17
src/templates/Template.php
Normal file
17
src/templates/Template.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\templates;
|
||||
|
||||
abstract class Template
|
||||
{
|
||||
|
||||
abstract static function getInputTemplate();
|
||||
|
||||
abstract static function getTextAreaTemplate();
|
||||
|
||||
abstract static function getSelectTemplate();
|
||||
|
||||
abstract static function getRadioTemplate();
|
||||
abstract static function getCheckBoxTemplate();
|
||||
abstract static function getButtonTemplate();
|
||||
}
|
37
src/templates/bootstrap5/Bootstrap5Template.php
Normal file
37
src/templates/bootstrap5/Bootstrap5Template.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\templates\bootstrap5;
|
||||
|
||||
use itguild\forms\templates\Template;
|
||||
|
||||
class Bootstrap5Template extends Template
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getInputTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{input}</div>";
|
||||
}
|
||||
public static function getTextAreaTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{textarea}</div>";
|
||||
}
|
||||
public static function getSelectTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{select}</div>";
|
||||
}
|
||||
public static function getRadioTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{radio}</div>";
|
||||
}
|
||||
public static function getCheckBoxTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{checkbox}</div>";
|
||||
}
|
||||
public static function getButtonTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}{button}</div>";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user