update 3
This commit is contained in:
parent
906a8fd739
commit
fb37605942
@ -12,6 +12,7 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"twbs/bootstrap": "5.0.2"
|
||||
"twbs/bootstrap": "5.0.2",
|
||||
"itguild/php-cg-select-v2": "^0.1.0"
|
||||
}
|
||||
}
|
||||
|
29
composer.lock
generated
29
composer.lock
generated
@ -4,8 +4,35 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "1b775feba4907582ffe0d09797b8c9bf",
|
||||
"content-hash": "44b947a1909c6ae745652c22e450679e",
|
||||
"packages": [
|
||||
{
|
||||
"name": "itguild/php-cg-select-v2",
|
||||
"version": "v0.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.itguild.info/apuc/php-cg-select-v2",
|
||||
"reference": "30c7844d96efaaab35b125db533a064f2254e169"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Itguild\\PhpCgSelectV2\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"ISC"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Kavalar",
|
||||
"email": "apuc06@mail.ru"
|
||||
}
|
||||
],
|
||||
"description": "Wrapper for CG-Select",
|
||||
"time": "2023-06-07T22:35:51+00:00"
|
||||
},
|
||||
{
|
||||
"name": "twbs/bootstrap",
|
||||
"version": "v5.0.2",
|
||||
|
13
example.php
13
example.php
@ -20,6 +20,9 @@ $form = new ActiveForm()
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Document</title>
|
||||
<link rel="stylesheet" href="vendor/twbs/bootstrap/dist/css/bootstrap.css">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
|
||||
<link href="https://cdn.jsdelivr.net/npm/vanillajs-datepicker/dist/css/datepicker.min.css" rel="stylesheet">
|
||||
<style>
|
||||
.bd-placeholder-img {
|
||||
font-size: 1.125rem;
|
||||
@ -158,12 +161,16 @@ $form = new ActiveForm()
|
||||
->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])
|
||||
$form->field(class: Select::class, name: 'select2', params: ['value' => 2, 'class' => "form-control"])
|
||||
->setOptions(['1' => 'bbb1', 2 => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
|
||||
->render();
|
||||
|
||||
|
||||
$form->field(\itguild\forms\inputs\Radio::class, name: "radio1", params: ["id" => "1"])->setLabel("sds?") ->render();
|
||||
$form->field(\itguild\forms\inputs\Radio::class, name: "radio1", params: ["id" => "3"])->render();
|
||||
$form->field(\itguild\forms\inputs\Checkbox::class, name: "checkbox1", params: ["value" => "1"])->render();
|
||||
$form->field(\itguild\forms\inputs\Checkbox::class, name: "checkbox2", params: ["value" => "1"])->render();
|
||||
$form->field(\itguild\forms\inputs\Button::class, name: "checkbox2", params: ["value" => "Submit"])->render();
|
||||
?>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="exampleInputPassword1">Password</label>
|
||||
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
|
||||
|
@ -2,10 +2,14 @@
|
||||
|
||||
namespace itguild\forms;
|
||||
|
||||
use itguild\forms\builders\ButtonBuilder;
|
||||
use itguild\forms\builders\CheckBoxBuilder;
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
use itguild\forms\builders\TextAreaBuilder;
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\Button;
|
||||
use itguild\forms\inputs\Checkbox;
|
||||
use itguild\forms\inputs\Select;
|
||||
use itguild\forms\inputs\TextArea;
|
||||
use itguild\forms\templates\Template;
|
||||
@ -41,6 +45,12 @@ class ActiveForm
|
||||
elseif ($class === TextArea::class){
|
||||
$this->fieldObject = TextAreaBuilder::build($name, $params);
|
||||
}
|
||||
elseif ($class === Checkbox::class) {
|
||||
$this->fieldObject = CheckBoxBuilder::build($name, $params);
|
||||
}
|
||||
elseif ($class === Button::class) {
|
||||
$this->fieldObject = ButtonBuilder::build($name, $params);
|
||||
}
|
||||
else {
|
||||
$this->fieldObject = new $class($name, $params);
|
||||
}
|
||||
|
16
src/builders/ButtonBuilder.php
Normal file
16
src/builders/ButtonBuilder.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
use itguild\forms\inputs\Button;
|
||||
class ButtonBuilder
|
||||
{
|
||||
|
||||
public static function build(string $name, array $params = [])
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
|
||||
return new Button(name: $name, value: $value, paramsArray: $params);
|
||||
}
|
||||
|
||||
}
|
16
src/builders/CheckBoxBuilder.php
Normal file
16
src/builders/CheckBoxBuilder.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
use itguild\forms\inputs\Checkbox;
|
||||
|
||||
|
||||
class CheckBoxBuilder
|
||||
{
|
||||
public static function build(string $name, array $params = [])
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
|
||||
return new Checkbox(name: $name, value: $value, paramsArray: $params);
|
||||
}
|
||||
}
|
@ -2,10 +2,14 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Template;
|
||||
|
||||
abstract class BaseInput
|
||||
{
|
||||
|
||||
protected $html;
|
||||
protected Template $inputTemplate;
|
||||
protected bool $hasLabel = false;
|
||||
protected string $html;
|
||||
protected string $labelTitle = "";
|
||||
|
||||
/**
|
||||
* @return void
|
||||
@ -18,8 +22,27 @@ abstract class BaseInput
|
||||
public abstract function create();
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->inputTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
|
||||
@ -12,8 +13,6 @@ class Button extends BaseInput
|
||||
private string $name;
|
||||
private string $value;
|
||||
private array $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
@ -25,6 +24,7 @@ class Button extends BaseInput
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->value = $value;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,10 +33,13 @@ class Button extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<button name='$this->name' $paramsString>$this->value</button>";
|
||||
$label = "";
|
||||
$button = "<button name='$this->name' $paramsString>$this->value</button>";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $button, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -49,19 +52,8 @@ class Button extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, string $value, array $paramsArray = []): void
|
||||
{
|
||||
$input = new self($name, $value, $paramsArray);
|
||||
$input->create()->render();
|
||||
}
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
$button = new self($name, $value, $paramsArray);
|
||||
$button->create()->render();
|
||||
}
|
||||
|
||||
}
|
37
src/inputs/CgSelectInput.php
Normal file
37
src/inputs/CgSelectInput.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
class CgSelectInput extends BaseInput
|
||||
{
|
||||
|
||||
private string $selector;
|
||||
private array $params;
|
||||
private $params;
|
||||
public function __construct(string $selector, array $params = [])
|
||||
{
|
||||
$this->selector = $selector;
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$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->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
|
||||
{
|
||||
$select = new self($name, $options, $value, $paramsArray);
|
||||
$select->create()->render();
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
|
||||
@ -12,8 +13,7 @@ class Checkbox extends BaseInput
|
||||
private string $name;
|
||||
private string $value;
|
||||
private array $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -21,11 +21,12 @@ class Checkbox extends BaseInput
|
||||
* @param string $value
|
||||
* @param array $paramsArray
|
||||
*/
|
||||
public function __construct(string $name, string $value, array $paramsArray = [])
|
||||
public function __construct(string $name, string $value = '', array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,10 +35,13 @@ class Checkbox extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
|
||||
$checkBox = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
|
||||
$label = "";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $checkBox, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -50,20 +54,8 @@ class Checkbox extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, string $value, array $paramsArray): void
|
||||
{
|
||||
$checkbox = new self($name, $value, $paramsArray);
|
||||
$checkbox->create()->render();
|
||||
$checkBox = new self($name, $value, $paramsArray);
|
||||
$checkBox->create()->render();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateOption;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
@ -12,13 +13,13 @@ class Radio extends BaseInput
|
||||
private $paramsArray;
|
||||
private $options = [];
|
||||
private $value;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
|
||||
public function __construct(string $name, array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,11 +29,13 @@ class Radio extends BaseInput
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$optionsString = $this->createOption($this->options, $this->value);
|
||||
$this->html = "<input name='$this->name' type='radio' $paramsString>$optionsString";
|
||||
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
|
||||
$label = "";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -43,21 +46,10 @@ class Radio extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, array $paramsArray = []): void
|
||||
{
|
||||
$label = new self($name, $paramsArray);
|
||||
$label->create()->render();
|
||||
$radioButton = new self($name, $paramsArray);
|
||||
$radioButton->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
|
@ -17,11 +17,9 @@ class Select extends BaseInput
|
||||
private $name;
|
||||
private $options;
|
||||
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private $value;
|
||||
private $paramsArray;
|
||||
private Template $selectTemplate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -36,7 +34,7 @@ class Select extends BaseInput
|
||||
$this->options = $options;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->selectTemplate = new SimpleTemplate();
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,7 +49,7 @@ class Select extends BaseInput
|
||||
if($this->hasLabel == true) {
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $select, $this->selectTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
@ -71,18 +69,6 @@ class Select extends BaseInput
|
||||
$select->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
* @return $this
|
||||
@ -94,14 +80,5 @@ class Select extends BaseInput
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->selectTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -14,9 +14,8 @@ class TextArea extends BaseInput
|
||||
private $name;
|
||||
private $value;
|
||||
private $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private Template $textareaTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
@ -27,7 +26,7 @@ class TextArea extends BaseInput
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->textareaTemplate = new SimpleTemplate();
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,7 +41,7 @@ class TextArea extends BaseInput
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{textarea}', $textarea, $this->textareaTemplate->getTextAreaTemplate());
|
||||
$this->html = str_replace('{input}', $textarea, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
@ -61,28 +60,4 @@ class TextArea extends BaseInput
|
||||
$textarea->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->textareaTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,12 +14,9 @@ class TextInput extends BaseInput
|
||||
|
||||
use CreateParams;
|
||||
private string $name;
|
||||
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private array $paramsArray;
|
||||
|
||||
private Template $inputTemplate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -63,28 +60,4 @@ class TextInput extends BaseInput
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->inputTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,25 +14,4 @@ class SimpleTemplate extends Template
|
||||
{
|
||||
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>";
|
||||
}
|
||||
}
|
@ -7,11 +7,5 @@ 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user