From 161bf8540f5d5528bc9a30751db5b9c729cb5410 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Wed, 24 Jun 2020 11:45:16 -0600 Subject: [PATCH] Add demo of dynamically added fileupload field reliant on AJAX request data being present to exist --- controllers/Countries.php | 24 ++++++++++++++++++++++++ models/Country.php | 4 ++++ models/country/fields.yaml | 9 +++++++++ 3 files changed, 37 insertions(+) diff --git a/controllers/Countries.php b/controllers/Countries.php index 084e2fb..e4d1843 100644 --- a/controllers/Countries.php +++ b/controllers/Countries.php @@ -1,5 +1,6 @@ isNested && $form->model instanceof \October\Test\Models\Country) { + $isActive = false; + if (Request::ajax()) { + $isActive = input('Country[is_active]', false); + } elseif ($form->model->exists) { + $isActive = $form->model->is_active; + } + + if ($isActive) { + $form->addFields([ + 'dynamic_flags' => [ + 'label' => 'Active Flags (Dynamic Fileupload Field)', + 'type' => 'fileupload', + 'mode' => 'image', + 'span' => 'right', + ], + ]); + } + } + } } diff --git a/models/Country.php b/models/Country.php index dd970d0..70f7b28 100644 --- a/models/Country.php +++ b/models/Country.php @@ -39,6 +39,10 @@ class Country extends Model 'conditions' => "type = 'general.type'" ], ]; + public $attachMany = [ + 'flags' => ['System\Models\File'], + 'dynamic_flags' => ['System\Models\File'], + ]; /** * Softly implement the TranslatableModel behavior. diff --git a/models/country/fields.yaml b/models/country/fields.yaml index 5c554f4..9f44cd9 100644 --- a/models/country/fields.yaml +++ b/models/country/fields.yaml @@ -16,11 +16,20 @@ fields: field: name type: slug + flags: + label: Flags (Static Fileupload Field) + type: fileupload + mode: image + span: full + is_active: label: Active comment: Check this box to hide some tabs, using depends and trigger API. type: checkbox + span: left default: true + attributes: + onchange: "$(this).request('form::onRefresh');" tabs: lazy: