From 62e1e8f3382391fd00adb5b4421015c2faa6e19b Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 3 Sep 2024 12:45:20 +0300 Subject: [PATCH] add file input --- examples/active_form.php | 2 +- index.php | 9 +++--- src/ActiveForm.php | 15 +++++++-- src/builders/FileBuilder.php | 15 +++++++++ src/inputs/File.php | 60 ++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 src/builders/FileBuilder.php create mode 100644 src/inputs/File.php diff --git a/examples/active_form.php b/examples/active_form.php index b163686..08327a3 100644 --- a/examples/active_form.php +++ b/examples/active_form.php @@ -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' => 'Логин', ]) diff --git a/index.php b/index.php index 26f5bb6..2a289d8 100644 --- a/index.php +++ b/index.php @@ -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(); \ No newline at end of file diff --git a/src/ActiveForm.php b/src/ActiveForm.php index e508ae0..ced9893 100755 --- a/src/ActiveForm.php +++ b/src/ActiveForm.php @@ -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; @@ -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(); diff --git a/src/builders/FileBuilder.php b/src/builders/FileBuilder.php new file mode 100644 index 0000000..40c11ba --- /dev/null +++ b/src/builders/FileBuilder.php @@ -0,0 +1,15 @@ +name = $name; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + $this->multiple = ''; + } + + /** + * @return self + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $input = "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'; + } +} \ No newline at end of file