diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..a8bf14ee
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+# OS Generated
+.DS_Store*
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f880401..631838c8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+# v0.3.0
+## 9/11/2015
+
+1. [](#improved)
+ * Refactored all the forms fields
+
# v0.2.1
## 08/24/2015
diff --git a/blueprints.yaml b/blueprints.yaml
index 9af90ba5..323adcaa 100644
--- a/blueprints.yaml
+++ b/blueprints.yaml
@@ -1,5 +1,5 @@
name: Form
-version: 0.2.1
+version: 0.3.0
description: Enables the forms handling
icon: check-square
author:
diff --git a/classes/form.php b/classes/form.php
index bd409bc3..9328490e 100644
--- a/classes/form.php
+++ b/classes/form.php
@@ -5,6 +5,7 @@
use Grav\Common\Grav;
use Grav\Common\GravTrait;
use Grav\Common\Page\Page;
+use RocketTheme\Toolbox\Event\Event;
class Form extends Iterator
{
@@ -88,7 +89,7 @@ public function post()
$action = \key($data);
$data = $data[$action];
}
- self::getGrav()->fireEvent('onFormProcessed', $this, $action, $data);
+ self::getGrav()->fireEvent('onFormProcessed', new Event(['form' => $this, 'action' => $action, 'params' => $data]));
}
} else {
// Default action.
diff --git a/form.php b/form.php
index f9998ecf..082383f4 100644
--- a/form.php
+++ b/form.php
@@ -8,6 +8,7 @@
use Grav\Common\Uri;
use Grav\Common\Twig;
use Grav\Plugin\Form;
+use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
class FormPlugin extends Plugin
@@ -85,17 +86,19 @@ public function onTwigSiteVariables()
/**
* Handle form processing instructions.
*
- * @param Form $form
- * @param string $task
- * @param string $params
+ * @param Event $event
*/
- public function onFormProcessed(Form $form, $task, $params)
+ public function onFormProcessed(Event $event)
{
+ $form = $event['form'];
+ $action = $event['action'];
+ $params = $event['params'];
+
if (!$this->active) {
return;
}
- switch ($task) {
+ switch ($action) {
case 'message':
$this->form->message = (string) $params;
break;
@@ -114,9 +117,16 @@ public function onFormProcessed(Form $form, $task, $params)
$uri = $this->grav['uri'];
$route = $uri->route() . ($route ? '/' . $route : '');
}
+
+ /** @var Twig $twig */
+ $twig = $this->grav['twig'];
+ $twig->twig_vars['form'] = $form;
+
/** @var Pages $pages */
$pages = $this->grav['pages'];
- $this->grav->page = $pages->dispatch($route, true);
+ $page = $pages->dispatch($route, true);
+ unset($this->grav['page']);
+ $this->grav['page'] = $page;
break;
case 'save':
$prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : '';
diff --git a/templates/forms/field.html.twig b/templates/forms/field.html.twig
new file mode 100644
index 00000000..aa029a05
--- /dev/null
+++ b/templates/forms/field.html.twig
@@ -0,0 +1,58 @@
+{% set originalValue = originalValue is defined ? originalValue : value %}
+{% set value = (value is null ? field.default : value) %}
+{# {% set vertical = field.style == 'vertical' %} #}
+{% set vertical = true %}
+
+{% block field %}
+
+{% endblock %}
diff --git a/templates/forms/fields/array/array.html.twig b/templates/forms/fields/array/array.html.twig
deleted file mode 100644
index aad7b536..00000000
--- a/templates/forms/fields/array/array.html.twig
+++ /dev/null
@@ -1,34 +0,0 @@
-{% set value = (value is null ? field.default : value) %}
-
-
diff --git a/templates/forms/fields/array/array.yaml b/templates/forms/fields/array/array.yaml
deleted file mode 100644
index 51c92fca..00000000
--- a/templates/forms/fields/array/array.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-field:
- array:
- node: array
- key: name
- fields:
- name:
- type: text
- validation:
- required: true
- value:
- type: text
diff --git a/templates/forms/fields/checkbox/checkbox.html.twig b/templates/forms/fields/checkbox/checkbox.html.twig
index dfd5dd0d..c6920a59 100644
--- a/templates/forms/fields/checkbox/checkbox.html.twig
+++ b/templates/forms/fields/checkbox/checkbox.html.twig
@@ -1,14 +1,29 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
+
- {{ field.label|e }} {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
+ {% endblock %}
+ />
+
+ {{ field.label|e }} {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
+
+{% endblock %}
diff --git a/templates/forms/fields/checkboxes/checkboxes.html.twig b/templates/forms/fields/checkboxes/checkboxes.html.twig
index 806a6957..a425e878 100644
--- a/templates/forms/fields/checkboxes/checkboxes.html.twig
+++ b/templates/forms/fields/checkboxes/checkboxes.html.twig
@@ -1,30 +1,34 @@
+{% extends "forms/field.html.twig" %}
+
+{% set originalValue = value %}
{% set value = (value is null ? field.default : value) %}
+{% if field.use == 'keys' and field.default %}
+ {% set value = field.default|merge(value) %}
+{% endif %}
-
-
-
- {% if field.help %}
- {{ field.label|e }}
- {% else %}
- {{ field.label|e }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
+{% block global_attributes %}
+ {{ parent() }}
+ data-grav-keys="{{ field.use == 'keys' ? 'true' : 'false' }}"
+ data-grav-field-name="{{ field.name|fieldName }}"
+{% endblock %}
+{% block input %}
+ {% for key, text in field.options %}
- {% for key, text in field.options %}
+ {% set id = field.name ~ '-' ~ key %}
+ {% set name = field.use == 'keys' ? key : id %}
+ {% set val = field.use == 'keys' ? '1' : key %}
{% set checked = (field.use == 'keys' ? value[key] : key in value) %}
- {% set name = field.name|fieldName ~ '[' ~ (field.use == 'keys' ? key : '') ~ ']' %}
- {% set key = (field.use == 'keys' ? '1' : key) %}
+
- {{ text|e }}
+ {{ text|e }}
- {% endfor %}
-
-
+ {% endfor %}
+{% endblock %}
diff --git a/templates/forms/fields/date/date.html.twig b/templates/forms/fields/date/date.html.twig
index c8b9121c..4e42985c 100644
--- a/templates/forms/fields/date/date.html.twig
+++ b/templates/forms/fields/date/date.html.twig
@@ -1,27 +1,8 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
-
-
+{% block input_attributes %}
+ type="date"
+ {% if field.validate.min %}min="{{ field.validate.min }}"{% endif %}
+ {% if field.validate.max %}max="{{ field.validate.max }}"{% endif %}
+ {{ parent() }}
+{% endblock %}
diff --git a/templates/forms/fields/datetime/datetime.html.twig b/templates/forms/fields/datetime/datetime.html.twig
index c8b9121c..4e955959 100644
--- a/templates/forms/fields/datetime/datetime.html.twig
+++ b/templates/forms/fields/datetime/datetime.html.twig
@@ -1,27 +1,18 @@
+{% extends "forms/field.html.twig" %}
+
+{% set default_php_dateformat = "d-m-Y H:i" %}
+{% set php_dateformat = config.system.pages.dateformat.default ?: default_php_dateformat %}
+{% set js_dateformat = admin.dateformat2Kendo(php_dateformat) %}
{% set value = (value is null ? field.default : value) %}
+{% set value = (value is null ? value : value|date(php_dateformat)) %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
+{% block input_attributes %}
+ type="text"
+ data-grav-field-datetime
+ data-date-formats="{{ {'format': js_dateformat} | json_encode | e('html_attr') }}"
+ {% if field.validate.min %}min="{{ (field.validate.min is null ? field.validate.min : field.validate.min|date(php_dateformat)) }}"{% endif %}
+ {% if field.validate.max %}max="{{ (field.validate.max is null ? field.validate.max : field.validate.max|date(php_dateformat)) }}"{% endif %}
+ {{ parent() }}
+{% endblock %}
-
-
-
diff --git a/templates/forms/fields/display/display.html.twig b/templates/forms/fields/display/display.html.twig
new file mode 100644
index 00000000..9ad1dee0
--- /dev/null
+++ b/templates/forms/fields/display/display.html.twig
@@ -0,0 +1,11 @@
+{% extends "forms/field.html.twig" %}
+
+{% block input %}
+
+ {% if field.markdown %}
+ {{ field.content|markdown }}
+ {% else %}
+ {{ field.content }}
+ {% endif %}
+
+{% endblock %}
diff --git a/templates/forms/fields/email/email.html.twig b/templates/forms/fields/email/email.html.twig
new file mode 100644
index 00000000..a1b89ffa
--- /dev/null
+++ b/templates/forms/fields/email/email.html.twig
@@ -0,0 +1,6 @@
+{% extends "forms/field.html.twig" %}
+
+{% block input_attributes %}
+ type="email"
+ {{ parent() }}
+{% endblock %}
\ No newline at end of file
diff --git a/templates/forms/fields/hidden/hidden.html.twig b/templates/forms/fields/hidden/hidden.html.twig
index 63372b51..1e90d477 100644
--- a/templates/forms/fields/hidden/hidden.html.twig
+++ b/templates/forms/fields/hidden/hidden.html.twig
@@ -1,3 +1,3 @@
{% set value = (value is null ? field.default : value) %}
-
+
diff --git a/templates/forms/fields/list/list.html.twig b/templates/forms/fields/list/list.html.twig
deleted file mode 100644
index 5cf71b37..00000000
--- a/templates/forms/fields/list/list.html.twig
+++ /dev/null
@@ -1,28 +0,0 @@
-{% if field.fields %}
-{% set scope = scope ~ field.name|fieldName ~ '..' %}
-{% set schema = {'features':{'options':{'sortable_root':false, 'sortable_children':false}, 'schema':field.fields}} %}
-
-
{{ field.add }}
-
-
-
-
-
- {% if value|length %}
- {% for key, data in value %}
-
- {% for field in field.fields %}
-
- {% if field.type %}
- {% set value = data.value(field.name) %}
- {% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
- {% endif %}
- {% endfor %}
-
- {% endfor %}
- {% endif %}
-
-
-
-
-{% endif %}
diff --git a/templates/forms/fields/pages/pages.html.twig b/templates/forms/fields/pages/pages.html.twig
deleted file mode 100644
index bd113af1..00000000
--- a/templates/forms/fields/pages/pages.html.twig
+++ /dev/null
@@ -1,47 +0,0 @@
-{% set value = (value is null ? field.default : value) %}
-{% set base_depth = 0 %}
-
-{% macro options(field, pages, value, depth) %}
-
- {% if field.show_root and depth == 0 %}
- / (Root)
- {% set depth = depth +1 %}
- {% endif %}
-
- {% set indent = depth == 0 ? '' : repeat('-',depth) ~ ' ' %}
-
- {% for page in pages %}
- {% if page.routable() or field.show_all %}
-
- {{indent}} {{ page.menu }}
-
- {% endif %}
- {% if page.count > 0 and (field.show_modular or not page.modular()) %}
-
- {{ _self.options(field, page,value, depth + 1) }}
-
- {% endif %}
- {% endfor %}
-{% endmacro %}
-
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
- {% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
- {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
- {% if field.validate.required in ['on', 'true', 1] %}required="required"{% endif %}
-
- {{ _self.options(field,pages,value, 0) }}
-
-
-
diff --git a/templates/forms/fields/password/password.html.twig b/templates/forms/fields/password/password.html.twig
index ad421f20..58047e86 100644
--- a/templates/forms/fields/password/password.html.twig
+++ b/templates/forms/fields/password/password.html.twig
@@ -1,24 +1,7 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
- {% endif %}
-
-
-
+{% block input_attributes %}
+ type="password"
+ class="input"
+ {{ parent() }}
+{% endblock %}
diff --git a/templates/forms/fields/radio/radio.html.twig b/templates/forms/fields/radio/radio.html.twig
index 868d6792..712b437e 100644
--- a/templates/forms/fields/radio/radio.html.twig
+++ b/templates/forms/fields/radio/radio.html.twig
@@ -1,29 +1,33 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
+{% set originalValue = value %}
+{% set value = (value is null ? field.default : value) %}
+{% if field.use == 'keys' and field.default %}
+ {% set value = field.default|merge(value) %}
+{% endif %}
+{% block global_attributes %}
+ {{ parent() }}
+ data-grav-keys="{{ field.use == 'keys' ? 'true' : 'false' }}"
+ data-grav-field-name="{{ field.name|fieldName }}"
+{% endblock %}
- {% for key, text in field.options %}
+{% block input %}
+ {% for key, text in field.options %}
{% set id = "radio_" ~ field.name ~ key %}
+ {% set name = field.use == 'keys' ? key : id %}
+ {% set val = field.use == 'keys' ? '1' : key %}
+ {% set checked = (field.use == 'keys' ? value[key] : key in value) %}
+
- {{ text }}
+ {{ text|e }}
- {% endfor %}
-
-
+ {% endfor %}
+{% endblock %}
diff --git a/templates/forms/fields/section/section.html.twig b/templates/forms/fields/section/section.html.twig
deleted file mode 100644
index 3347a7e0..00000000
--- a/templates/forms/fields/section/section.html.twig
+++ /dev/null
@@ -1,22 +0,0 @@
-{% if field.title %}
-{{ field.title }}
-{% endif %}
-
-{% if field.text %}
-{{ field.text }}
-{% endif %}
-
-{% if field.underline %}
-
-{% endif %}
-
-{% if field.fields %}
-
- {% for field in field.fields %}
- {% if field.type %}
- {% set value = data.value(field.name) %}
- {% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
- {% endif %}
- {% endfor %}
-
-{% endif %}
diff --git a/templates/forms/fields/select/select.html.twig b/templates/forms/fields/select/select.html.twig
index d8e79423..df5edbc2 100644
--- a/templates/forms/fields/select/select.html.twig
+++ b/templates/forms/fields/select/select.html.twig
@@ -1,27 +1,22 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
+{% block global_attributes %}
+ data-grav-selectize="{{ (field.selectize is defined ? field.selectize : {})|json_encode()|e('html_attr') }}"
+ {{ parent() }}
+{% endblock %}
-
-
+
{% for key, text in field.options %}
-
- {{ text }}
-
+ {% if grav.twig.twig.filters['tu'] is defined %}{{ text|tu }}{% else %}{{ text|t }}{% endif %}
{% endfor %}
-
-
+
+{% endblock %}
diff --git a/templates/forms/fields/tab/tab.html.twig b/templates/forms/fields/tab/tab.html.twig
deleted file mode 100644
index 0c7c8e70..00000000
--- a/templates/forms/fields/tab/tab.html.twig
+++ /dev/null
@@ -1,10 +0,0 @@
-{% if field.fields %}
-
- {% for field in field.fields %}
- {% if field.type %}
- {% set value = data.value(field.name) %}
- {% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
- {% endif %}
- {% endfor %}
-
-{% endif %}
diff --git a/templates/forms/fields/tabs/tabs.html.twig b/templates/forms/fields/tabs/tabs.html.twig
deleted file mode 100644
index 6b0e0368..00000000
--- a/templates/forms/fields/tabs/tabs.html.twig
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
diff --git a/templates/forms/fields/text/text.html.twig b/templates/forms/fields/text/text.html.twig
index cec6986c..2b96ebea 100644
--- a/templates/forms/fields/text/text.html.twig
+++ b/templates/forms/fields/text/text.html.twig
@@ -1,28 +1,6 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
-
+{% block input_attributes %}
+ type="text"
+ {{ parent() }}
+{% endblock %}
diff --git a/templates/forms/fields/textarea/textarea.html.twig b/templates/forms/fields/textarea/textarea.html.twig
index 8e6a6233..ec510e14 100644
--- a/templates/forms/fields/textarea/textarea.html.twig
+++ b/templates/forms/fields/textarea/textarea.html.twig
@@ -1,17 +1,21 @@
-{% set value = (value is null ? field.default : value) %}
+{% extends "forms/field.html.twig" %}
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
-
-
+{% block input %}
+
+
+
+{% endblock %}
diff --git a/templates/forms/fields/themeselect/themeselect.html.twig b/templates/forms/fields/themeselect/themeselect.html.twig
deleted file mode 100644
index 2dfb247b..00000000
--- a/templates/forms/fields/themeselect/themeselect.html.twig
+++ /dev/null
@@ -1,35 +0,0 @@
-{% set value = (value is null ? field.default : value) %}
-
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
- {% for option, name in field.options %}
-
- {{ name }}
-
- {% endfor %}
- {% for theme in admin.themes %}
- {% set blueprints = theme.blueprints %}
-
- {{ blueprints.get('name') }}
-
- {% endfor %}
-
-
-
diff --git a/templates/forms/fields/toggle/toggle.html.twig b/templates/forms/fields/toggle/toggle.html.twig
deleted file mode 100644
index 37f172c2..00000000
--- a/templates/forms/fields/toggle/toggle.html.twig
+++ /dev/null
@@ -1,34 +0,0 @@
-{% set value = (value is null ? field.default : value) %}
-{% set value = (value is same as(false) ? 0 : value) %}
-
-
diff --git a/templates/forms/fields/unset/unset.html.twig b/templates/forms/fields/unset/unset.html.twig
deleted file mode 100644
index e69de29b..00000000
diff --git a/templates/forms/fields/upload/upload.html.twig b/templates/forms/fields/upload/upload.html.twig
deleted file mode 100644
index ec46cb82..00000000
--- a/templates/forms/fields/upload/upload.html.twig
+++ /dev/null
@@ -1,16 +0,0 @@
-{% set value = (value is null ? field.default : value) %}
-
-
-
-
- {% if field.help %}
- {{ field.label }}
- {% else %}
- {{ field.label }}
- {% endif %}
- {{ field.validate.required in ['on', 'true', 1] ? '* ' }}
-
-
-
-
-