diff --git a/checkPOST.php b/checkPOST.php new file mode 100644 index 0000000..2d12f42 --- /dev/null +++ b/checkPOST.php @@ -0,0 +1,21 @@ + $_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); diff --git a/composer.json b/composer.json index a1f8c3b..1144337 100644 --- a/composer.json +++ b/composer.json @@ -11,5 +11,7 @@ "name": "nikita" } ], - "require": {} + "require": { + "twbs/bootstrap": "5.0.2" + } } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..4356d2b --- /dev/null +++ b/composer.lock @@ -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" +} diff --git a/example.php b/example.php new file mode 100644 index 0000000..47258f2 --- /dev/null +++ b/example.php @@ -0,0 +1,193 @@ + + + + + + + + Document + + + + +
+ +
+
+ + + + +
+ + +
+
+
+ +
+ + beginForm('some'); ?> + 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(); + + + ?> +
+ + +
+
+ + +
+ + endForm(); ?> +
+ + +
+ +
+
+ + diff --git a/form.json b/form.json new file mode 100644 index 0000000..17a7003 --- /dev/null +++ b/form.json @@ -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" +} \ No newline at end of file diff --git a/index.php b/index.php index d248bfe..cdf103a 100644 --- a/index.php +++ b/index.php @@ -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("
{label}{input}
") ->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(); \ No newline at end of file + ->render(); +$form->button('button', "Кнопка", ["id" => "button"]); +$form->endForm(); + diff --git a/src/ActiveForm.php b/src/ActiveForm.php index 3caa6a1..7b38d98 100644 --- a/src/ActiveForm.php +++ b/src/ActiveForm.php @@ -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 "
"; + } + + /** + * @return void + */ + public function endForm(): void + { + echo "
"; + + } + 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(); diff --git a/src/Form.php b/src/Form.php index 285423e..63f5be0 100644 --- a/src/Form.php +++ b/src/Form.php @@ -18,7 +18,7 @@ class Form { */ public function beginForm(string $action): void { - echo "
"; + echo ""; } /** diff --git a/src/builders/TextAreaBuilder.php b/src/builders/TextAreaBuilder.php new file mode 100644 index 0000000..c092a38 --- /dev/null +++ b/src/builders/TextAreaBuilder.php @@ -0,0 +1,19 @@ +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 = ""; + $label = ""; + $select = ""; if($this->hasLabel == true) { - $this->html = " $this->html"; + $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; + } } \ No newline at end of file diff --git a/src/inputs/TextArea.php b/src/inputs/TextArea.php index 74b4cd7..e6992ac 100644 --- a/src/inputs/TextArea.php +++ b/src/inputs/TextArea.php @@ -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 = ""; + $label = ""; + $textarea = ""; if($this->hasLabel == true) { - $this->html = " $this->html"; + $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; + } + } \ No newline at end of file diff --git a/src/inputs/TextInput.php b/src/inputs/TextInput.php index 7e51e5e..757df91 100644 --- a/src/inputs/TextInput.php +++ b/src/inputs/TextInput.php @@ -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 = ""; + $label = ""; + $input = ""; if($this->hasLabel == true) { - $this->html = " $this->html"; + $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; + } + } \ No newline at end of file diff --git a/src/templates/Simple/SimpleTemplate.php b/src/templates/Simple/SimpleTemplate.php new file mode 100644 index 0000000..4b572d8 --- /dev/null +++ b/src/templates/Simple/SimpleTemplate.php @@ -0,0 +1,38 @@ +{label}
{input}"; + } + + public static function getTextAreaTemplate(): string + { + return "
{label}
{textarea}
"; + } + public static function getSelectTemplate(): string + { + return "
{label}
{select}
"; + } + public static function getRadioTemplate(): string + { + return "
{label}
{radio}
"; + } + public static function getCheckBoxTemplate(): string + { + return "
{label}
{checkbox}
"; + } + public static function getButtonTemplate(): string + { + return "
{label}
{button}
"; + } +} \ No newline at end of file diff --git a/src/templates/Template.php b/src/templates/Template.php new file mode 100644 index 0000000..98dd369 --- /dev/null +++ b/src/templates/Template.php @@ -0,0 +1,17 @@ +{label}{input}"; + } + public static function getTextAreaTemplate(): string + { + return "
{label}{textarea}
"; + } + public static function getSelectTemplate(): string + { + return "
{label}{select}
"; + } + public static function getRadioTemplate(): string + { + return "
{label}{radio}
"; + } + public static function getCheckBoxTemplate(): string + { + return "
{label}{checkbox}
"; + } + public static function getButtonTemplate(): string + { + return "
{label}{button}
"; + } +} \ No newline at end of file