This commit is contained in:
kali 2024-03-20 18:31:49 +03:00
parent fd39285e41
commit 5ecf87df75
15 changed files with 207 additions and 76 deletions

View File

@ -13,6 +13,7 @@
],
"require": {
"twbs/bootstrap": "5.0.2",
"itguild/php-cg-select-v2": "^0.1.0"
"itguild/php-cg-select-v2": "^0.1.0",
"craft-group/phroute": "^2.1"
}
}

49
composer.lock generated
View File

@ -4,8 +4,55 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "44b947a1909c6ae745652c22e450679e",
"content-hash": "36625cc8108fc002b61761ac864a999a",
"packages": [
{
"name": "craft-group/phroute",
"version": "v2.1.2",
"source": {
"type": "git",
"url": "https://github.com/apuc/phroute.git",
"reference": "49180faae85e0ba3b0165901ad46e08508c81fac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/apuc/phroute/zipball/49180faae85e0ba3b0165901ad46e08508c81fac",
"reference": "49180faae85e0ba3b0165901ad46e08508c81fac",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
"satooshi/php-coveralls": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Phroute\\Phroute\\": "src/Phroute"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"description": "Fast, fully featured restful request router for PHP",
"keywords": [
"router",
"routing"
],
"support": {
"source": "https://github.com/apuc/phroute/tree/v2.1.2"
},
"time": "2022-12-17T18:27:33+00:00"
},
{
"name": "itguild/php-cg-select-v2",
"version": "v0.1",

View File

@ -8,6 +8,7 @@
"type": "textInput",
"name": "email",
"placeholder": "Email",
"id": "emailId",
"label": {
"title": "Email", "for": "emailId", "class": "form-label"
},
@ -20,11 +21,37 @@
"class": "form-control",
"style": "color:RED;"
},
{
"type": "select",
"name": "select_city",
"class": "form-control",
"id": "cityId",
"label": {
"title": "Выберите город", "for": "cityId", "class": "form-label"
},
"value": 2,
"options": {"1": "Москва", "2": "Донецк", "3": "Ростов"}
},
{
"type": "textArea",
"name": "description",
"class": "form-control",
"placeholder": "Описание"
},
{
"type": "checkbox",
"name": "agree",
"class": "",
"id": "agreeId",
"label": {
"title": "Согласен с правилами", "for": "agreeId", "class": "form-label"
}
},
{
"type": "button",
"value": "Проверить",
"name": "btn",
"class": "btn btn-primary"
}
]
}

View File

@ -1,55 +1,19 @@
<?php
use itguild\forms\Form;
use \itguild\forms\inputs\TextInput;
use \itguild\forms\inputs\RadioButton;
use \itguild\forms\inputs\Select;
use \itguild\forms\ActiveForm;
ini_set("display_errors", true);
ini_set("display_errors", 1);
error_reporting(-1);
require_once "vendor/autoload.php";
$formData = [];
use Phroute\Phroute\RouteCollector;
$form = new Form();
$form->beginForm("checkPOST.php");
$form->textInput("phone", ['placeholder' => 'Введите телефон', 'class' => 'form-group']);
$form->textInput("email", ['placeholder' => 'Введите email']);
$form->textInput("name", ['type' => 'number']);
$form->textInput("lastname");
$form->checkBox("Button", "1", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "2", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "3", ["class" => 'sdsa', "id" => "b1"]);
$form->label("RadioButton", ["for" => "r2"]);
$form->radio("radio1", ["id" => "r2"]);
$form->label("RadioButton", ["for" => "r3"]);
$form->radio("radio1", ["id" => "r3"]);
$form->textarea("textarea", "dsadasads", ["id" => "t1"]);
$form->select("select", ["class1" => "option", "class2" => "b2", "class3" => "b4"], 'class2', ["id" => "s1"]);
$router = new RouteCollector();
$router->get('/', [\itguild\forms\app\controllers\Main::class, 'indexAction']);
$router->get('/example', [\itguild\forms\app\controllers\Main::class, 'exampleAction']);
$activeForm = new ActiveForm();
$activeForm->field(class: TextInput::class, name: 'one', params: ["style" => "color:RED;display:flex"])
->render();
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());
$activeForm->field(class: TextInput::class, name: 'two', params: [])
->render();
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->setTemplate("<div class='form-group'>{label}{input}</div>")
->setLabel("bbb222bbb")
->render();
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setLabel("bbbbbb")
->render();
$activeForm->field(Select::class, 'select2', ['value' => 2])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->render();
$form->button('button', "Кнопка", ["id" => "button"]);
$form->endForm();
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
// Print out the value returned from the dispatched function
echo $response;

55
old_index.php Normal file
View File

@ -0,0 +1,55 @@
<?php
use itguild\forms\Form;
use \itguild\forms\inputs\TextInput;
use \itguild\forms\inputs\RadioButton;
use \itguild\forms\inputs\Select;
use \itguild\forms\ActiveForm;
ini_set("display_errors", true);
error_reporting(-1);
require_once "vendor/autoload.php";
$formData = [];
$form = new Form();
$form->beginForm("checkPOST.php");
$form->textInput("phone", ['placeholder' => 'Введите телефон', 'class' => 'form-group']);
$form->textInput("email", ['placeholder' => 'Введите email']);
$form->textInput("name", ['type' => 'number']);
$form->textInput("lastname");
$form->checkBox("Button", "1", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "2", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "3", ["class" => 'sdsa', "id" => "b1"]);
$form->label("RadioButton", ["for" => "r2"]);
$form->radio("radio1", ["id" => "r2"]);
$form->label("RadioButton", ["for" => "r3"]);
$form->radio("radio1", ["id" => "r3"]);
$form->textarea("textarea", "dsadasads", ["id" => "t1"]);
$form->select("select", ["class1" => "option", "class2" => "b2", "class3" => "b4"], 'class2', ["id" => "s1"]);
$activeForm = new ActiveForm();
$activeForm->field(class: TextInput::class, name: 'one', params: ["style" => "color:RED;display:flex"])
->render();
$activeForm->field(class: TextInput::class, name: 'two', params: [])
->render();
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->setTemplate("<div class='form-group'>{label}{input}</div>")
->setLabel("bbb222bbb")
->render();
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setLabel("bbbbbb")
->render();
$activeForm->field(Select::class, 'select2', ['value' => 2])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->render();
$form->button('button', "Кнопка", ["id" => "button"]);
$form->endForm();

11
routing.php Normal file
View File

@ -0,0 +1,11 @@
<?php
ini_set("display_errors", 1);
error_reporting(-1);
require_once "vendor/autoload.php";
use Phroute\Phroute\RouteCollector;
$router = new RouteCollector();
$router->get('/', [\itguild\forms\app\controllers\Main::class, 'indexAction']);

View File

@ -0,0 +1,18 @@
<?php
namespace itguild\forms\app\controllers;
class Main
{
public function indexAction(): void
{
echo 123;
}
public function exampleAction()
{
echo "example";
}
}

View File

@ -6,9 +6,9 @@ use itguild\forms\inputs\Checkbox;
class CheckBoxBuilder
{
public static function build(string $name, array $params = [])
public static function build(string $name, array $params = []): Checkbox
{
$value = $params['value'] ?? null;
$value = $params['value'] ?? "";
unset($params['value']);
return new Checkbox(name: $name, value: $value, paramsArray: $params);

View File

@ -10,6 +10,7 @@ abstract class BaseInput
protected bool $hasLabel = false;
protected string $html = '';
protected string|Label $label = "";
protected string $labelString = "";
/**
* @return void
@ -38,6 +39,18 @@ abstract class BaseInput
return $this;
}
protected function createLabel()
{
if($this->hasLabel) {
if(is_string($this->label)){
$this->labelString = "<label>$this->label</label>";
}
else {
$this->labelString= $this->label->create()->fetch();
}
}
}
/**
* @param $template
* @return $this

View File

@ -34,12 +34,12 @@ class Button extends BaseInput
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$button = "<input type='button' name='$this->name' $paramsString>$this->value";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}
$button = "<input type='button' value='$this->value' name='$this->name' $paramsString>";
$this->createLabel();
$this->html = str_replace('{input}', $button, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}

View File

@ -37,11 +37,11 @@ class Checkbox extends BaseInput
$paramsString = $this->createParams($this->paramsArray);
$checkBox = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
$label = "";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}
$this->createLabel();
$this->html = str_replace('{input}', $checkBox, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}

View File

@ -46,11 +46,11 @@ class Select extends BaseInput
$optionsString = $this->createOption($this->options, $this->value);
$label = "";
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}
$this->createLabel();
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;

View File

@ -37,12 +37,11 @@ class TextArea extends BaseInput
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$textarea = "<textarea name='$this->name' $paramsString>$this->value</textarea>";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}
$this->createLabel();
$this->html = str_replace('{input}', $textarea, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}

View File

@ -38,17 +38,11 @@ class TextInput extends BaseInput
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$input = "<input name='$this->name' $paramsString >";
if($this->hasLabel == true) {
if(is_string($this->label)){
$label = "<label>$this->labelTitle</label>";
}
else {
$label = $this->create()->fetch();
}
}
$this->createLabel();
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}

View File

@ -4,6 +4,7 @@ namespace itguild\forms\mappers;
use itguild\forms\builders\Builder;
use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\CheckBoxBuilder;
use itguild\forms\builders\LabelBuilder;
use itguild\forms\builders\RadioButtonBuilder;
use itguild\forms\builders\SelectBuilder;
@ -20,7 +21,8 @@ class JsonInputMapper
"textArea" => TextAreaBuilder::class,
"radio" => RadioButtonBuilder::class,
"select" => SelectBuilder::class,
"button" => ButtonBuilder::class
"button" => ButtonBuilder::class,
"checkbox" => CheckBoxBuilder::class,
];
}