Skip to content

Commit

Permalink
Add support for category specific fields
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jtojnar committed Aug 22, 2017
1 parent 0390b03 commit ccf3aff
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/components/TeamForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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']);
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions app/presenters/TeamPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions app/templates/Team/edit.latte
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

{block content}
{control teamForm}
{/block}
{block scripts}
<script src="{$basePath}/js/form.js"></script>
3 changes: 3 additions & 0 deletions app/templates/Team/register.latte
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

{block content}
{control teamForm}
{/block}
{block scripts}
<script src="{$basePath}/js/form.js"></script>
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions www/js/form.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit ccf3aff

Please sign in to comment.