Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
55624f9b67 | |||
3dd054b298 | |||
46564f9450 | |||
dc7e2c80a4 | |||
53e5fadfc1 | |||
9dfb6a52c3 | |||
45e57367d3 | |||
ddb17cc473 | |||
62e1e8f338 | |||
0501718421 | |||
0f00069165 |
@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "itguild/forms",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"itguild\\forms\\": "src/"
|
||||
@ -8,9 +9,11 @@
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Kavalar"
|
||||
"name": "Kavalar",
|
||||
"email": "apuc06@mail.ru"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"twbs/bootstrap": "5.0.2",
|
||||
"itguild/php-cg-select-v2": "^0.1.0",
|
||||
|
@ -3,7 +3,7 @@ require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm("/some_action");
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [
|
||||
$form->field(class: \itguild\forms\inputs\File::class, name: "login", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Логин',
|
||||
])
|
||||
|
@ -4,11 +4,12 @@ require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm("/some_action");
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [
|
||||
$form->field(class: \itguild\forms\inputs\File::class, name: "login", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Логин',
|
||||
'placeholder' => 'Файл',
|
||||
])
|
||||
->setLabel("Логин")
|
||||
->render();
|
||||
->setLabel("Файл")
|
||||
->setMultiple()
|
||||
->render();
|
||||
|
||||
$form->endForm();
|
@ -4,6 +4,7 @@ namespace itguild\forms;
|
||||
|
||||
use itguild\forms\builders\ButtonBuilder;
|
||||
use itguild\forms\builders\CheckBoxBuilder;
|
||||
use itguild\forms\builders\FileBuilder;
|
||||
use itguild\forms\builders\HiddenBuilder;
|
||||
use itguild\forms\builders\RadioButtonBuilder;
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
@ -13,6 +14,7 @@ use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\Button;
|
||||
use itguild\forms\inputs\Checkbox;
|
||||
use itguild\forms\inputs\File;
|
||||
use itguild\forms\inputs\Hidden;
|
||||
use itguild\forms\inputs\RadioButton;
|
||||
use itguild\forms\inputs\Select;
|
||||
@ -29,9 +31,9 @@ class ActiveForm
|
||||
* @param string $action
|
||||
* @return void
|
||||
*/
|
||||
public function beginForm(string $action): void
|
||||
public function beginForm(string $action, string $enctype = 'application/x-www-form-urlencoded'): void
|
||||
{
|
||||
echo "<form method='POST' action='$action'>";
|
||||
echo "<form method='POST' action='$action' enctype='$enctype'>";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,7 +45,7 @@ class ActiveForm
|
||||
|
||||
}
|
||||
|
||||
public function field($class, string $name, array $params = [])
|
||||
public function field($class, string $name, array $params = []): static
|
||||
{
|
||||
if ($class === Select::class){
|
||||
$this->fieldObject = SelectBuilder::build($name, $params);
|
||||
@ -66,10 +68,12 @@ class ActiveForm
|
||||
elseif ($class === Hidden::class){
|
||||
$this->fieldObject = HiddenBuilder::build($name, $params);
|
||||
}
|
||||
elseif ($class === File::class){
|
||||
$this->fieldObject = FileBuilder::build($name, $params);
|
||||
}
|
||||
else {
|
||||
$this->fieldObject = new $class($name, $params);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -94,6 +98,13 @@ class ActiveForm
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMultiple(): self
|
||||
{
|
||||
$this->fieldObject->setMultiple();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$this->fieldObject->create();
|
||||
|
16
src/builders/EmailBuilder.php
Executable file
16
src/builders/EmailBuilder.php
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
|
||||
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\EmailInput;
|
||||
use itguild\forms\inputs\TextInput;
|
||||
|
||||
class EmailBuilder implements Builder
|
||||
{
|
||||
public static function build(string $name, array $params = []): BaseInput
|
||||
{
|
||||
return new EmailInput(name: $name, paramsArray: $params);
|
||||
}
|
||||
}
|
15
src/builders/FileBuilder.php
Normal file
15
src/builders/FileBuilder.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\File;
|
||||
|
||||
class FileBuilder implements Builder
|
||||
{
|
||||
public static function build(string $name, array $params = []): BaseInput
|
||||
{
|
||||
return new File(name: $name, paramsArray: $params);
|
||||
}
|
||||
}
|
@ -8,11 +8,29 @@ use itguild\forms\inputs\Select;
|
||||
class SelectBuilder
|
||||
{
|
||||
|
||||
public static function build(string $name, array $params = [])
|
||||
// public static function build(string $name, array $params = [])
|
||||
// {
|
||||
// $value = $params['value'] ?? null;
|
||||
// unset($params['value']);
|
||||
// $options = $params['options'] ?? [];
|
||||
// unset($params['options']);
|
||||
//
|
||||
// return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
|
||||
// }
|
||||
|
||||
public static function build(string $name, array $params = []): Select
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
if (isset($params['prompt'])) {
|
||||
$options['prompt'] = $params['prompt'] ?? null;
|
||||
foreach ($params['options'] as $key => $val) {
|
||||
$options[$key] = $val;
|
||||
}
|
||||
} else {
|
||||
$options = $params['options'] ?? [];
|
||||
}
|
||||
|
||||
unset($params['options']);
|
||||
|
||||
return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
|
||||
|
62
src/inputs/EmailInput.php
Executable file
62
src/inputs/EmailInput.php
Executable file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
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 EmailInput extends BaseInput
|
||||
{
|
||||
|
||||
use CreateParams;
|
||||
private string $name;
|
||||
private array $paramsArray;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $paramsArray
|
||||
*/
|
||||
public function __construct(string $name, array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return self
|
||||
*/
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$label = "";
|
||||
$input = "<input name='$this->name' type='email' $paramsString >";
|
||||
|
||||
$this->createLabel();
|
||||
|
||||
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $this->labelString, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $paramsArray
|
||||
* @return void
|
||||
*/
|
||||
public static function build(string $name, array $paramsArray = []): void
|
||||
{
|
||||
$input = new self($name, $paramsArray);
|
||||
$input->create()->render();
|
||||
|
||||
}
|
||||
|
||||
}
|
60
src/inputs/File.php
Normal file
60
src/inputs/File.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
|
||||
class File extends BaseInput
|
||||
{
|
||||
use CreateParams;
|
||||
private string $name;
|
||||
private array $paramsArray;
|
||||
private string $multiple;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $paramsArray
|
||||
*/
|
||||
|
||||
public function __construct(string $name, array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
$this->multiple = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return self
|
||||
*/
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$input = "<input type='file' name='$this->name' $this->multiple $paramsString >";
|
||||
|
||||
$this->createLabel();
|
||||
|
||||
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $this->labelString, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $paramsArray
|
||||
* @return void
|
||||
*/
|
||||
public static function build(string $name, array $paramsArray = []): void
|
||||
{
|
||||
$input = new self($name, $paramsArray);
|
||||
$input->create()->render();
|
||||
}
|
||||
|
||||
public function setMultiple(): void
|
||||
{
|
||||
$this->multiple = 'multiple';
|
||||
}
|
||||
}
|
@ -73,9 +73,9 @@ class Select extends BaseInput
|
||||
* @param $options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions($options)
|
||||
public function setOptions($options): static
|
||||
{
|
||||
$this->options = array_merge($options, $this->options);
|
||||
$this->options = $options + $this->options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace itguild\forms\traits;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
|
||||
trait CreateOption
|
||||
{
|
||||
/**
|
||||
@ -9,13 +11,65 @@ trait CreateOption
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
// public function createOption(array $options, $value = null): string
|
||||
// {
|
||||
// $optionsString = "";
|
||||
// if (is_array($value)) {
|
||||
// foreach ($options as $val => $title) {
|
||||
// $selected = in_array($title, $value) ? "selected" : "";
|
||||
// $optionsString .= "<option $selected value='$val'>$title</option>";
|
||||
// }
|
||||
// } else {
|
||||
// foreach ($options as $val => $title) {
|
||||
// if (is_numeric($value)) {
|
||||
// $selected = (int)$value === $val ? "selected" : "";
|
||||
// } else {
|
||||
// $selected = $value === $val ? "selected" : "";
|
||||
// }
|
||||
// $optionsString .= "<option $selected value='$val'>$title</option>";
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return $optionsString;
|
||||
// }
|
||||
|
||||
public function createOption(array $options, $value = null): string
|
||||
{
|
||||
$optionsString = "";
|
||||
foreach ($options as $val => $title){
|
||||
$selected = (int)$value === $val ? "selected" : "";
|
||||
if ($value) {
|
||||
if (isset($options['prompt'])) {
|
||||
$prompt = $options['prompt'];
|
||||
$optionsString .= "<option value='''>$prompt</option>";
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
foreach ($options as $val => $title) {
|
||||
if ($val === 'prompt') continue;
|
||||
$selected = in_array($title, $value) ? "selected" : "";
|
||||
$optionsString .= "<option $selected value='$val'>$title</option>";
|
||||
}
|
||||
} else {
|
||||
foreach ($options as $val => $title) {
|
||||
if ($val === 'prompt') continue;
|
||||
if (is_numeric($value)) {
|
||||
$selected = (int)$value === $val ? "selected" : "";
|
||||
} else {
|
||||
$selected = $value === $val ? "selected" : "";
|
||||
}
|
||||
$optionsString .= "<option $selected value='$val'>$title</option>";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isset($options['prompt'])) {
|
||||
$prompt = $options['prompt'];
|
||||
$optionsString .= "<option value='' selected='selected'>$prompt</option>";
|
||||
}
|
||||
foreach ($options as $val => $title) {
|
||||
if ($val === 'prompt') continue;
|
||||
$optionsString .= "<option value='$val'>$title</option>";
|
||||
}
|
||||
}
|
||||
|
||||
return $optionsString;
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ trait CreateParams
|
||||
{
|
||||
$paramsString = "";
|
||||
foreach($data as $key => $param){
|
||||
if(is_string($param)){
|
||||
if(is_string($param) || is_numeric($param)){
|
||||
$paramsString .= $key . "='" . $param . "'";
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user