Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for form level configuration #406

Merged
merged 2 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,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;
Expand Down Expand Up @@ -461,18 +461,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' => []
];
Expand Down Expand Up @@ -520,7 +520,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());
}

/**
Expand All @@ -534,15 +534,15 @@ protected function addErrorClass()
$errorBag = $this->parent->getErrorBag();

if ($errors && $errors->hasBag($errorBag) && $errors->getBag($errorBag)->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)) {
$fieldClass .= ' ' . $fieldErrorClass;
$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)) {
Expand Down Expand Up @@ -576,9 +576,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')) {
Expand Down Expand Up @@ -927,4 +927,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);
}
}
25 changes: 23 additions & 2 deletions src/Kris/LaravelFormBuilder/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class Form
'url' => null
];

/**
* Form specific configuration.
*
* @var array
*/
protected $formConfig;

/**
* Additional data which can be used to build fields.
*
Expand Down Expand Up @@ -492,6 +499,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.
*
Expand Down Expand Up @@ -850,7 +871,7 @@ public function getTemplatePrefix()
return $this->templatePrefix;
}

return $this->formHelper->getConfig('template_prefix');
return $this->getConfig('template_prefix');
}

/**
Expand Down Expand Up @@ -946,7 +967,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'));
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Kris/LaravelFormBuilder/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down