This commit is contained in:
kali 2024-03-18 17:52:16 +03:00
parent a73c5e8170
commit bd456e6331
15 changed files with 532 additions and 23 deletions

21
checkPOST.php Normal file
View File

@ -0,0 +1,21 @@
<?php
$formData = [
"phone" => $_POST['phone'],
"email" => $_POST['email'],
"name" => $_POST['name'],
"lastname" => $_POST['lastname'],
"button" => $_POST['Button'],
"radio1" => $_POST['radio1'],
"textarea" => $_POST['textarea'],
"select" => $_POST['select'],
"one" => $_POST['one'],
"two" => $_POST['two'],
"radio2" => $_POST['radio2'],
"select2" => $_POST['select2']
];
$jsonData = json_encode($formData);
$file = "form.json";
file_put_contents($file, $jsonData);

View File

@ -11,5 +11,7 @@
"name": "nikita"
}
],
"require": {}
"require": {
"twbs/bootstrap": "5.0.2"
}
}

69
composer.lock generated Normal file
View File

@ -0,0 +1,69 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1b775feba4907582ffe0d09797b8c9bf",
"packages": [
{
"name": "twbs/bootstrap",
"version": "v5.0.2",
"source": {
"type": "git",
"url": "https://github.com/twbs/bootstrap.git",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twbs/bootstrap/zipball/688bce4fa695cc360a0d084e34f029b0c192b223",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223",
"shasum": ""
},
"replace": {
"twitter/bootstrap": "self.version"
},
"type": "library",
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mark Otto",
"email": "markdotto@gmail.com"
},
{
"name": "Jacob Thornton",
"email": "jacobthornton@gmail.com"
}
],
"description": "The most popular front-end framework for developing responsive, mobile first projects on the web.",
"homepage": "https://getbootstrap.com/",
"keywords": [
"JS",
"css",
"framework",
"front-end",
"mobile-first",
"responsive",
"sass",
"web"
],
"support": {
"issues": "https://github.com/twbs/bootstrap/issues",
"source": "https://github.com/twbs/bootstrap/tree/v5.0.2"
},
"time": "2021-06-22T18:29:16+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

193
example.php Normal file
View File

@ -0,0 +1,193 @@
<?php
ini_set("display_errors", 1);
error_reporting(-1);
require_once "vendor/autoload.php";
use itguild\forms\ActiveForm;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\Select;
$form = new ActiveForm()
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="vendor/twbs/bootstrap/dist/css/bootstrap.css">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
.b-example-divider {
width: 100%;
height: 3rem;
background-color: rgba(0, 0, 0, .1);
border: solid rgba(0, 0, 0, .15);
border-width: 1px 0;
box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15);
}
.b-example-vr {
flex-shrink: 0;
width: 1.5rem;
height: 100vh;
}
.bi {
vertical-align: -.125em;
fill: currentColor;
}
.nav-scroller {
position: relative;
z-index: 2;
height: 2.75rem;
overflow-y: hidden;
}
.nav-scroller .nav {
display: flex;
flex-wrap: nowrap;
padding-bottom: 1rem;
margin-top: -1px;
overflow-x: auto;
text-align: center;
white-space: nowrap;
-webkit-overflow-scrolling: touch;
}
.btn-bd-primary {
--bd-violet-bg: #712cf9;
--bd-violet-rgb: 112.520718, 44.062154, 249.437846;
--bs-btn-font-weight: 600;
--bs-btn-color: var(--bs-white);
--bs-btn-bg: var(--bd-violet-bg);
--bs-btn-border-color: var(--bd-violet-bg);
--bs-btn-hover-color: var(--bs-white);
--bs-btn-hover-bg: #6528e0;
--bs-btn-hover-border-color: #6528e0;
--bs-btn-focus-shadow-rgb: var(--bd-violet-rgb);
--bs-btn-active-color: var(--bs-btn-hover-color);
--bs-btn-active-bg: #5a23c8;
--bs-btn-active-border-color: #5a23c8;
}
.bd-mode-toggle {
z-index: 1500;
}
.bd-mode-toggle .dropdown-menu .active .bi {
display: block !important;
}
.form-control-dark {
border-color: var(--bs-gray);
}
.form-control-dark:focus {
border-color: #fff;
box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .25);
}
.text-small {
font-size: 85%;
}
.dropdown-toggle:not(:focus) {
outline: 0;
}
</style>
</head>
<body>
<main>
<div class="container">
<header class="d-flex flex-wrap align-items-center justify-content-center justify-content-md-between py-3 mb-4 border-bottom">
<div class="col-md-3 mb-2 mb-md-0">
<a href="/" class="d-inline-flex link-body-emphasis text-decoration-none">
<svg class="bi" width="40" height="32" role="img" aria-label="Bootstrap">
<use xlink:href="#bootstrap"/>
</svg>
</a>
</div>
<ul class="nav col-12 col-md-auto mb-2 justify-content-center mb-md-0">
<li><a href="#" class="nav-link px-2 link-secondary">Home</a></li>
<li><a href="#" class="nav-link px-2">Features</a></li>
<li><a href="#" class="nav-link px-2">Pricing</a></li>
<li><a href="#" class="nav-link px-2">FAQs</a></li>
<li><a href="#" class="nav-link px-2">About</a></li>
</ul>
<div class="col-md-3 text-end">
<button type="button" class="btn btn-outline-primary me-2">Login</button>
<button type="button" class="btn btn-primary">Sign-up</button>
</div>
</header>
</div>
<div class="container contact-form">
<?php $form->beginForm('some'); ?>
<?php
$form->field(TextInput::class, name: "email", params: [
'class' => "form-control",
'placeholder' => 'Email'
])
->setTemplate(\itguild\forms\templates\bootstrap5\Bootstrap5Template::class)
->setLabel("Email")
->render();
$form->field(TextArea::class, name: "phone", params: ['class' => "form-control", 'placeholder' => 'phone', 'value'=> 'asd'])->setTemplate(\itguild\forms\templates\bootstrap5\Bootstrap5Template::class)->setLabel("Message")->render();
$form->field(class: Select::class, name: 'select2', params: ['value' => 2])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])->render();
?>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<?php $form->endForm(); ?>
</div>
<div class="container">
<footer class="py-3 my-4">
<ul class="nav justify-content-center border-bottom pb-3 mb-3">
<li class="nav-item"><a href="#" class="nav-link px-2 text-body-secondary">Home</a></li>
<li class="nav-item"><a href="#" class="nav-link px-2 text-body-secondary">Features</a></li>
<li class="nav-item"><a href="#" class="nav-link px-2 text-body-secondary">Pricing</a></li>
<li class="nav-item"><a href="#" class="nav-link px-2 text-body-secondary">FAQs</a></li>
<li class="nav-item"><a href="#" class="nav-link px-2 text-body-secondary">About</a></li>
</ul>
<p class="text-center text-body-secondary">&copy; 2024 Company, Inc</p>
</footer>
</div>
</main>
</body>
</html>

18
form.json Normal file
View File

@ -0,0 +1,18 @@
{
"textInpt": {
"name": "email",
"placeholder": "Email",
"id": "EmailId"
},
"email": "dsadas@dsadsa",
"name": "1",
"lastname": "edas",
"button": "3",
"radio1": "on",
"textarea": "dsadasads",
"select": "class3",
"one": "ads",
"two": "dsa",
"radio2": "on",
"select2": "1"
}

View File

@ -11,8 +11,10 @@ error_reporting(-1);
require_once "vendor/autoload.php";
$formData = [];
$form = new Form();
$form->beginForm("tsad");
$form->beginForm("checkPOST.php");
$form->textInput("phone", ['placeholder' => 'Введите телефон', 'class' => 'form-group']);
$form->textInput("email", ['placeholder' => 'Введите email']);
$form->textInput("name", ['type' => 'number']);
@ -21,30 +23,33 @@ $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("name1", ["id" => "r2"]);
$form->radio("radio1", ["id" => "r2"]);
$form->label("RadioButton", ["for" => "r3"]);
$form->radio("name1", ["id" => "r3"]);
$form->radio("radio1", ["id" => "r3"]);
$form->textarea("textarea", "dsadasads", ["id" => "t1"]);
$form->select("select", ["class1" => "option", "class2" => "b2", "class3" => "b4"], 'class2', ["id" => "s1"]);
$form->button('button', "Кнопка", ["id" => "button"]);
$form->endForm();
$activeForm = new ActiveForm();
$activeForm->field(class: TextInput::class, name: 'nnn', params: ["style" => "color:RED;display:flex"])
$activeForm->field(class: TextInput::class, name: 'one', params: ["style" => "color:RED;display:flex"])
->render();
$activeForm->field(class: TextInput::class, name: 'bbb', params: [])
$activeForm->field(class: TextInput::class, name: 'two', params: [])
->render();
$activeForm->field(class: Radio::class, name: 'nameee',params: ["style" => "color:RED;display:flex"])
$activeForm->field(class: Radio::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: Radio::class, name: 'nameee',params: ["style" => "color:RED;display:flex"])
$activeForm->field(class: Radio::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setLabel("bbbbbb")
->render();
$activeForm->field(Select::class, 'ddd', ['value' => 2])
$activeForm->field(Select::class, 'select2', ['value' => 2])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->render();
->render();
$form->button('button', "Кнопка", ["id" => "button"]);
$form->endForm();

View File

@ -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();

View File

@ -18,7 +18,7 @@ class Form {
*/
public function beginForm(string $action): void
{
echo "<form action='$action'>";
echo "<form method='POST' action='$action'>";
}
/**

View 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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View 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>";
}
}

View 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();
}

View 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>";
}
}