From ccf3affa43ae9aeeb216e2e443f1042977b0ecf8 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 16 Aug 2017 10:38:31 +0200 Subject: [PATCH] Add support for category specific fields Some fields are only applicable to a subset of the categories. This commit hides the irrelevant fields using JavaScript and filters them out on the server side. --- app/components/TeamForm.php | 5 +++++ app/presenters/TeamPresenter.php | 22 ++++++++++++++++++++++ app/templates/Team/edit.latte | 3 +++ app/templates/Team/register.latte | 3 +++ docs/configuration.md | 1 + www/js/form.js | 25 +++++++++++++++++++++++++ 6 files changed, 59 insertions(+) create mode 100644 www/js/form.js diff --git a/app/components/TeamForm.php b/app/components/TeamForm.php index a34887a..8287d6f 100644 --- a/app/components/TeamForm.php +++ b/app/components/TeamForm.php @@ -10,6 +10,7 @@ use Nette\Forms\Form; use Nette\Utils\Callback; use Nette\Utils\Html; +use Nette\Utils\Json; class TeamForm extends UI\Form { /** @var array */ @@ -159,6 +160,10 @@ public function addCustomFields($fields, $container) { if (isset($field['description'])) { $input->setOption('description', $field['description'][$locale]); } + + if (isset($field['applicableCategories'])) { + $input->getControlPrototype()->{'data-applicable-categories'} = Json::encode($field['applicableCategories']); + } } } } diff --git a/app/presenters/TeamPresenter.php b/app/presenters/TeamPresenter.php index 0d35a02..6888e94 100644 --- a/app/presenters/TeamPresenter.php +++ b/app/presenters/TeamPresenter.php @@ -223,6 +223,8 @@ public function processTeamForm(Nette\Forms\Controls\SubmitButton $button) { /** @var string $password */ $password = null; + $this->cleanNonApplicableFields($form); + if ($this->action === 'edit') { $id = (int) $this->getParameter('id'); $team = $this->teams->getById($id); @@ -413,6 +415,26 @@ public function processTeamForm(Nette\Forms\Controls\SubmitButton $button) { } } + public function cleanNonApplicableFields(Nette\Forms\Form $form) { + $category = $form['category']->getValue(); + + $teamFields = $this->presenter->context->parameters['entries']['fields']['team']; + foreach ($teamFields as $name => $field) { + if (isset($field['applicableCategories']) && !in_array($category, $field['applicableCategories'], true)) { + $form[$name]->setValue(null); + } + } + + $personFields = $this->presenter->context->parameters['entries']['fields']['person']; + foreach ($form['persons']->values as $member) { + foreach ($personFields as $name => $field) { + if (isset($field['applicableCategories']) && !in_array($category, $field['applicableCategories'], true)) { + $form['persons'][$name]->setValue(null); + } + } + } + } + public function createComponentTeamListFilterForm() { $form = new Form(); $renderer = $form->renderer; diff --git a/app/templates/Team/edit.latte b/app/templates/Team/edit.latte index 4356fcb..40c20fc 100644 --- a/app/templates/Team/edit.latte +++ b/app/templates/Team/edit.latte @@ -2,3 +2,6 @@ {block content} {control teamForm} +{/block} +{block scripts} + diff --git a/app/templates/Team/register.latte b/app/templates/Team/register.latte index c82a493..6b82d38 100644 --- a/app/templates/Team/register.latte +++ b/app/templates/Team/register.latte @@ -2,3 +2,6 @@ {block content} {control teamForm} +{/block} +{block scripts} + diff --git a/docs/configuration.md b/docs/configuration.md index 7792b66..4a953ae 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -170,6 +170,7 @@ fields: * `type` – Each field will need to declare its type, see [field types](#field-types). * `label` – Some fields may define a default label (`sportident`, `country`) but otherwise you should define one for each language. * `private` – By default, the value of every will be displayed at the team list publicly. You can set it to `false` to prevent leaking personal information or items only relevant for organizers. +* `applicableCategories` – A list of categories to show this field in. If not present, every category is implied. ###### Field types diff --git a/www/js/form.js b/www/js/form.js new file mode 100644 index 0000000..3714680 --- /dev/null +++ b/www/js/form.js @@ -0,0 +1,25 @@ +$(function(){ + var conditionalFields = []; + $('[data-applicable-categories]').each(function() { + conditionalFields.push([ + $(this).parents('.form-group'), + JSON.parse(this.getAttribute('data-applicable-categories')) + ]); + }); + + var categoryField = $('#frm-teamForm-category'); + + function categoryChanged() { + var category = categoryField.val(); + $.each(conditionalFields, function() { + if (this[1].indexOf(category) === -1) { + $(this[0]).hide(); + } else { + $(this[0]).show(); + } + }); + }; + + categoryField.change(categoryChanged); + categoryChanged(); +});