Skip to content

Commit

Permalink
Fixed readonly on switch and checkbox field types (octobercms#3750)
Browse files Browse the repository at this point in the history
Credit to @gergo85
  • Loading branch information
gergo85 authored and LukeTowers committed Sep 3, 2018
1 parent 55575e3 commit 6998883
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions modules/backend/classes/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,15 @@ public function options($value = null)
* - radio - creates a set of radio buttons.
* - checkbox - creates a single checkbox.
* - checkboxlist - creates a checkbox list.
* - switch - creates a switch field.
* @param string $type Specifies a render mode as described above
* @param array $config A list of render mode specific config.
*/
public function displayAs($type, $config = [])
{
$this->type = strtolower($type) ?: $this->type;
$this->config = $this->evalConfig($config);

return $this;
}

Expand Down Expand Up @@ -422,6 +424,7 @@ public function getAttributes($position = 'field', $htmlBuild = true)
{
$result = array_get($this->attributes, $position, []);
$result = $this->filterAttributes($result, $position);

return $htmlBuild ? Html::attributes($result) : $result;
}

Expand All @@ -445,6 +448,10 @@ protected function filterAttributes($attributes, $position = 'field')

if ($position == 'field' && $this->readOnly) {
$attributes = $attributes + ['readonly' => 'readonly'];

if ($this->type == 'checkbox' || $this->type == 'switch') {
$attributes = $attributes + ['onclick' => 'return false;'];
}
}

return $attributes;
Expand Down Expand Up @@ -507,9 +514,7 @@ protected function filterTriggerAttributes($attributes, $position = 'field')
'data-trigger-closest-parent' => 'form'
];

$attributes = $attributes + $newAttributes;

return $attributes;
return $attributes + $newAttributes;
}

/**
Expand Down Expand Up @@ -548,8 +553,7 @@ protected function filterPresetAttributes($attributes, $position = 'field')
$newAttributes['data-input-preset-prefix-input'] = $prefixInput;
}

$attributes = $attributes + $newAttributes;
return $attributes;
return $attributes + $newAttributes;
}

/**
Expand Down

0 comments on commit 6998883

Please sign in to comment.