Added Multiple Input Types

This commit is contained in:
Florian Bouillon 2019-04-25 16:15:37 +02:00
parent 64a836c0e4
commit d73566086c
6 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace AdminPanel\Form;
class ArrayInput extends AbstractInput
{
public function __construct()
{
$this->options = array(
'elements' => array()
);
$this->setOption("type", "text");
}
public function getOptionsList(): array
{
return array_merge(
parent::getOptionsList(),
array(
"array_type",
"item",
"script"
)
);
}
public function getAttributesList(): array
{
return array(
'name',
'id',
);
}
public function setOption(string $name, $value)
{
if ($name === "array_type") {
$clas = $value;
$input = new $clas();
$input->setOption("id", $this->attributes["name"] . "-__unique__");
$input->setOption("name", $this->attributes["name"] . "[__unique__]");
$this->options["data-element"] = $input;
} elseif ($name === "item") {
foreach ($value as $vname => $vvalue) {
$this->options["data-element"]->setOption($vname, $vvalue);
}
}
parent::setOption($name, $value);
}
public function render(): string
{
if (array_key_exists("array_type", $this->options) &&
array_key_exists("value", $this->options)
) {
$clas = $this->options["array_type"];
if (isset($this->options["value"])) {
foreach ($this->options["value"] as $key => $value) {
$input = new $clas();
$input->setOption("id", $this->attributes["name"] . "-" . $key);
$input->setOption("name", $this->attributes["name"] . "[" . $key . "]");
$input->setOption("value", $value);
$this->options["elements"][] = $input;
}
}
}
$this->attributes["data-element"] = "<span>" . $this->options["data-element"]->render() . "</span>";
return parent::render();
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace AdminPanel\Form;
class DatalistInput extends AbstractInput
{
public function getOptionsList(): array
{
return array_merge(
parent::getOptionsList(),
array(
'list'
)
);
}
public function setOption(string $name, $value)
{
if ($name === "name") {
$this->attributes["list"] = $name . "_list";
}
parent::setOption($name, $value);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace AdminPanel\Form;
class NumberInput extends AbstractInput
{
public function getAttributesList(): array
{
return array_merge(
parent::getAttributesList(),
array(
'min',
'max'
)
);
}
}

View File

@ -0,0 +1,15 @@
{% extends "@AdminPanel/form/_default.twig" %}
{% set name = "div" %}
{% block content %}
{% for item in options.elements %}
<div>
{{item.render()|raw}}
</div>
{% endfor %}
{% endblock %}
{% block after %}
<button type="button" data-for="{{ attributes.name }}">Ajouter</button>
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends "@AdminPanel/form/_default.twig" %}
{% block after %}
<datalist id="{{ attributes.list }}">
{% for element in options.list %}
<option>{{element}}</option>
{% endfor %}
</datalist>
{% endblock %}

View File

@ -0,0 +1 @@
{% extends "@AdminPanel/form/_default.twig" %}