From 88da9c6dd7d6fda36beca524acee9424904c20a1 Mon Sep 17 00:00:00 2001 From: Bruno Beghelli Date: Mon, 26 Feb 2018 11:09:09 -0300 Subject: [PATCH] Allow form level config --- .../LaravelFormBuilder/Fields/FormField.php | 34 ++++++++++++------- src/Kris/LaravelFormBuilder/Form.php | 25 ++++++++++++-- src/Kris/LaravelFormBuilder/FormHelper.php | 17 ++++++++-- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/Kris/LaravelFormBuilder/Fields/FormField.php b/src/Kris/LaravelFormBuilder/Fields/FormField.php index 9ead83a8..d1b54f65 100644 --- a/src/Kris/LaravelFormBuilder/Fields/FormField.php +++ b/src/Kris/LaravelFormBuilder/Fields/FormField.php @@ -287,7 +287,7 @@ protected function prepareOptions(array $options = []) if ($this->getOption('required') === true || isset($parsedRules['required'])) { $lblClass = $this->getOption('label_attr.class', ''); - $requiredClass = $helper->getConfig('defaults.required_class', 'required'); + $requiredClass = $this->getConfig('defaults.required_class', 'required'); if (! str_contains($lblClass, $requiredClass)) { $lblClass .= ' '.$requiredClass; @@ -460,18 +460,18 @@ protected function getDefaults() private function allDefaults() { return [ - 'wrapper' => ['class' => $this->formHelper->getConfig('defaults.wrapper_class')], - 'attr' => ['class' => $this->formHelper->getConfig('defaults.field_class')], + 'wrapper' => ['class' => $this->getConfig('defaults.wrapper_class')], + 'attr' => ['class' => $this->getConfig('defaults.field_class')], 'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [ - 'class' => $this->formHelper->getConfig('defaults.help_block_class') + 'class' => $this->getConfig('defaults.help_block_class') ]], 'value' => null, 'default_value' => null, 'label' => null, 'label_show' => true, 'is_child' => false, - 'label_attr' => ['class' => $this->formHelper->getConfig('defaults.label_class')], - 'errors' => ['class' => $this->formHelper->getConfig('defaults.error_class')], + 'label_attr' => ['class' => $this->getConfig('defaults.label_class')], + 'errors' => ['class' => $this->getConfig('defaults.error_class')], 'rules' => [], 'error_messages' => [] ]; @@ -519,7 +519,7 @@ public function setValue($value) */ private function setTemplate() { - $this->template = $this->formHelper->getConfig($this->getTemplate(), $this->getTemplate()); + $this->template = $this->getConfig($this->getTemplate(), $this->getTemplate()); } /** @@ -532,7 +532,7 @@ protected function addErrorClass() $errors = $this->parent->getRequest()->session()->get('errors'); if ($errors && $errors->has($this->getNameKey())) { - $fieldErrorClass = $this->formHelper->getConfig('defaults.field_error_class'); + $fieldErrorClass = $this->getConfig('defaults.field_error_class'); $fieldClass = $this->getOption('attr.class'); if ($fieldErrorClass && !str_contains($fieldClass, $fieldErrorClass)) { @@ -540,7 +540,7 @@ protected function addErrorClass() $this->setOption('attr.class', $fieldClass); } - $wrapperErrorClass = $this->formHelper->getConfig('defaults.wrapper_error_class'); + $wrapperErrorClass = $this->getConfig('defaults.wrapper_error_class'); $wrapperClass = $this->getOption('wrapper.class'); if ($wrapperErrorClass && $this->getOption('wrapper') && !str_contains($wrapperClass, $wrapperErrorClass)) { @@ -574,9 +574,9 @@ protected function setDefaultOptions(array $options = []) */ protected function setDefaultClasses(array $options = []) { - $wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.wrapper_class', ''); - $label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.label_class', ''); - $field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.field_class', ''); + $wrapper_class = $this->getConfig('defaults.' . $this->type . '.wrapper_class', ''); + $label_class = $this->getConfig('defaults.' . $this->type . '.label_class', ''); + $field_class = $this->getConfig('defaults.' . $this->type . '.field_class', ''); $defaults = []; if ($wrapper_class && !array_get($options, 'wrapper.class')) { @@ -925,4 +925,14 @@ public function getRawValue() { return $this->rawValue; } + + /** + * Get config from the form. + * + * @return mixed + */ + private function getConfig($key = null, $default = null) + { + return $this->parent->getConfig($key, $default); + } } diff --git a/src/Kris/LaravelFormBuilder/Form.php b/src/Kris/LaravelFormBuilder/Form.php index c21f9e9a..e64a4528 100644 --- a/src/Kris/LaravelFormBuilder/Form.php +++ b/src/Kris/LaravelFormBuilder/Form.php @@ -50,6 +50,13 @@ class Form 'url' => null ]; + /** + * Form specific configuration. + * + * @var array + */ + protected $formConfig; + /** * Additional data which can be used to build fields. * @@ -480,6 +487,20 @@ public function setFormOption($option, $value) return $this; } + /** + * Get the passed config key using the custom + * form config, if any. + * + * @param string $key + * @param mixed $default + * + * @return mixed + */ + public function getConfig($key = null, $default = null) + { + return $this->formHelper->getConfig($key, $default, $this->formConfig); + } + /** * Set form options. * @@ -837,7 +858,7 @@ public function getTemplatePrefix() return $this->templatePrefix; } - return $this->formHelper->getConfig('template_prefix'); + return $this->getConfig('template_prefix'); } /** @@ -933,7 +954,7 @@ protected function render($options, $fields, $showStart, $showFields, $showEnd) */ protected function getTemplate() { - return $this->getTemplatePrefix() . $this->getFormOption('template', $this->formHelper->getConfig('form')); + return $this->getTemplatePrefix() . $this->getFormOption('template', $this->getConfig('form')); } /** diff --git a/src/Kris/LaravelFormBuilder/FormHelper.php b/src/Kris/LaravelFormBuilder/FormHelper.php index 2aa21ab2..d13c1d4e 100644 --- a/src/Kris/LaravelFormBuilder/FormHelper.php +++ b/src/Kris/LaravelFormBuilder/FormHelper.php @@ -104,11 +104,24 @@ public function __construct(View $view, Translator $translator, array $config = /** * @param string $key * @param string $default + * @param mixed $customConfig * @return mixed */ - public function getConfig($key, $default = null) + public function getConfig($key = null, $default = null, $customConfig = null) { - return array_get($this->config, $key, $default); + $config = $this->config; + if (is_array($customConfig)) { + $config = array_replace_recursive($config, $customConfig); + } + + if ($key) { + $returnConfig = array_get($config, $key, $default); + } + else { + $returnConfig = $config; + } + + return $returnConfig; } /**