Skip to content

Fix bugs in ChoiceType's name #715

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 34 additions & 13 deletions src/Kris/LaravelFormBuilder/Fields/ChoiceType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Kris\LaravelFormBuilder\Fields;
namespace Kris\LaravelFormBuilder\Fields;

use Illuminate\Support\Arr;

Expand Down Expand Up @@ -28,25 +28,46 @@ protected function getTemplate()
}

/**
* Determine which choice type to use.
*
* @return string
* @inheritdoc
*/
protected function determineChoiceField()
protected function prepareOptions(array $options = [])
{
$expanded = $this->options['expanded'];
$multiple = $this->options['multiple'];
parent::prepareOptions($options);

$expanded = $this->getOption('expanded', false);
$multiple = $this->getOption('multiple', false);

Arr::forget($this->options, ['attr.multiple']);

if (!$expanded && $multiple) {
$this->options['attr']['multiple'] = true;
$this->setOption('attr.multiple', true);
}

if ($expanded && !$multiple) {
return $this->choiceType = 'radio';
if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
$this->name = $this->name . '[]';
$this->setOption('tmp.multipleBracesSet', true);
}

if ($expanded && $multiple) {
return $this->choiceType = 'checkbox';
if (!$this->getOption('attr.multiple') && $this->getOption('tmp.multipleBracesSet')) {
$this->name = preg_replace('/\[\]$/u', '', $this->name);
$this->setOption('tmp.multipleBracesSet', false);
}

return $this->options;
}

/**
* Determine which choice type to use.
*
* @return string
*/
protected function determineChoiceField()
{
$expanded = $this->getOption('expanded', false);
$multiple = $this->getOption('multiple', false);

if ($expanded) {
return $this->choiceType = $multiple ? 'checkbox' : 'radio';
}

return $this->choiceType = 'select';
Expand Down Expand Up @@ -107,7 +128,7 @@ protected function buildCheckableChildren($fieldType)
{
$multiple = $this->getOption('multiple') ? '[]' : '';

$attr = $this->options['attr']?? [];
$attr = $this->options['attr'] ?? [];
$attr = Arr::except($attr, ['class', 'multiple', 'id', 'name']);
foreach ((array)$this->options['choices'] as $key => $choice) {
$id = str_replace('.', '_', $this->getNameKey()) . '_' . $key;
Expand Down
7 changes: 6 additions & 1 deletion src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ protected function prepareOptions(array $options = [])
$this->setOption('tmp.multipleBracesSet', true);
}

if (!$this->getOption('attr.multiple') && $this->getOption('tmp.multipleBracesSet')) {
$this->name = preg_replace('/\[\]$/u', '', $this->name);
$this->setOption('tmp.multipleBracesSet', false);
}

if ($this->parent->haveErrorsEnabled()) {
$this->addErrorClass();
}
Expand Down Expand Up @@ -406,7 +411,7 @@ protected function normalizeRules($rules)
*
* @param array $first first set of rules
* @param array $second second set of rules
* @return array merged set of rules without duplicates
* @return array merged set of rules without duplicates
*/
protected function mergeRules($first, $second)
{
Expand Down
9 changes: 8 additions & 1 deletion src/Kris/LaravelFormBuilder/Fields/ParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ public function setValue($val)
*/
public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
{
$options['children'] = $this->children;
if (!empty($options)) {
$this->prepareOptions($options);

$this->createChildren();
}

$this->setOption('children', $this->getChildren());

return parent::render($options, $showLabel, $showField, $showError);
}

Expand Down
36 changes: 16 additions & 20 deletions src/Kris/LaravelFormBuilder/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function rebuildForm()
if ($this->isPlain()) {
foreach ($this->fields as $name => $field) {
// Remove any temp variables added in previous instance
$options = Arr::except($field->getOptions(), 'tmp');
$options = Arr::except($field->getOptions(), 'tmp');
$this->add($name, $field->getType(), $options);
}
} else {
Expand All @@ -187,7 +187,7 @@ public function rebuildForm()
*/
protected function isPlain()
{
if($this->formBuilder === null) {
if ($this->formBuilder === null) {
throw new \RuntimeException('FormBuilder is not set');
}

Expand Down Expand Up @@ -251,7 +251,6 @@ protected function addField(FormField $field, $modify = false)
$this->preventDuplicate($field->getRealName());
}


if ($field->getType() == 'file') {
$this->formOptions['files'] = true;
}
Expand Down Expand Up @@ -399,8 +398,12 @@ public function modify($name, $type = 'text', array $options = [], $overwriteOpt
{
// If we don't want to overwrite options, we merge them with old options.
if ($overwriteOptions === false && $this->has($name)) {
$fieldOptions = $this->getField($name)->getOptions();

Arr::forget($fieldOptions, ['tmp']);

$options = $this->formHelper->mergeOptions(
$this->getField($name)->getOptions(),
$fieldOptions,
$options
);
}
Expand Down Expand Up @@ -878,7 +881,7 @@ public function getData($name = null, $default = null)
* will be switched to protected in 1.7.
* @param $data
* @return $this
**/
*/
public function addData(array $data)
{
foreach ($data as $key => $value) {
Expand Down Expand Up @@ -988,7 +991,7 @@ public function setTranslationTemplate($template)
* Render the form.
*
* @param array $options
* @param string $fields
* @param string[] $fields
* @param bool $showStart
* @param bool $showFields
* @param bool $showEnd
Expand Down Expand Up @@ -1052,16 +1055,9 @@ protected function getTemplate()
*/
protected function getUnrenderedFields()
{
$unrenderedFields = [];

foreach ($this->fields as $field) {
if (!$field->isRendered()) {
$unrenderedFields[] = $field;
continue;
}
}

return $unrenderedFields;
return array_filter($this->fields, function ($field) {
return !$field->isRendered();
});
}

/**
Expand All @@ -1086,9 +1082,7 @@ protected function preventDuplicate($name)
*/
protected function getFieldType($type)
{
$fieldType = $this->formHelper->getFieldType($type);

return $fieldType;
return $this->formHelper->getFieldType($type);
}

/**
Expand Down Expand Up @@ -1480,6 +1474,7 @@ public function getFilters()
public function lockFiltering()
{
$this->lockFiltering = true;

return $this;
}

Expand All @@ -1491,6 +1486,7 @@ public function lockFiltering()
public function unlockFiltering()
{
$this->lockFiltering = false;

return $this;
}

Expand All @@ -1502,7 +1498,7 @@ public function unlockFiltering()
*/
public function isFilteringLocked()
{
return !$this->lockFiltering ? false : true;
return (bool)$this->lockFiltering;
}

/**
Expand Down
Loading