From 6ce374f3fbd2c3a1ffbb258dedec3968ad3ee4aa Mon Sep 17 00:00:00 2001 From: Neil Jennings Date: Wed, 26 Jun 2019 16:46:27 +0100 Subject: [PATCH] form view option, check displayed in templates before using validation rules on submission --- src/Libraries/Blocks/Form.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Libraries/Blocks/Form.php b/src/Libraries/Blocks/Form.php index e9987c1..e1251f9 100644 --- a/src/Libraries/Blocks/Form.php +++ b/src/Libraries/Blocks/Form.php @@ -4,6 +4,7 @@ use CoasterCms\Helpers\Cms\Email; use CoasterCms\Helpers\Cms\View\FormWrap; use CoasterCms\Facades\FormMessage; +use Illuminate\Support\Facades\Cache; use PageBuilder; use CoasterCms\Models\Block; use CoasterCms\Models\BlockFormRule; @@ -22,6 +23,11 @@ class Form extends AbstractBlock */ public static $blockSettings = ['Manage form input validation rules' => 'themes/forms']; + /** + * @var string + */ + protected $_displayedTemplatesKey; + /** * Repeater constructor. * @param Block $block @@ -30,6 +36,7 @@ public function __construct(Block $block) { parent::__construct($block); $this->_displayViewDirs[] = 'forms'; + $this->_displayedTemplatesKey = 'displayed_form_' . $this->_block->id . '_templates'; } /** @@ -42,6 +49,12 @@ public function display($content, $options = []) { $formData = $this->_defaultData($content); $view = !empty($options['view']) ? $options['view'] : $formData->template; + if ($view) { + $formTemplates = Cache::get($this->_displayedTemplatesKey, []); + $formTemplates[] = $view; + $formTemplates = array_unique($formTemplates); + Cache::put($this->_displayedTemplatesKey, $formTemplates, 60 + abs((int)config('coaster::frontend.cache'))); + } return FormWrap::view($this->_block, $options, $this->displayView($view), ['form_data' => $formData]); } @@ -98,7 +111,10 @@ public function submission($formData) $form_settings = $this->_defaultData($form_settings); if (!empty($formData['form_template'])) { - $form_settings->template = $formData['form_template']; + $formTemplates = Cache::get($this->_displayedTemplatesKey, []); + if (in_array($formData['form_template'], $formTemplates)) { + $form_settings->template = $formData['form_template']; + } } unset($formData['form_template']);