Skip to content

Commit 514266d

Browse files
committed
Update the _addButtonClasses method to not add duplicate btn-xxxx classes.
1 parent 0649df0 commit 514266d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/View/Helper/BootstrapFormHelper.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ class BootstrapFormHelper extends FormHelper {
116116
public $horizontal = false ;
117117
public $inline = false ;
118118

119-
private $buttonTypes = ['default', 'primary', 'info', 'success', 'warning', 'danger', 'link'] ;
120-
private $buttonSizes = ['xs', 'sm', 'lg'] ;
121-
122119
/**
123120
*
124121
* Replace the templates with the ones specified by newTemplates, call the specified function

src/View/Helper/BootstrapTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ protected function _addButtonClasses ($options) {
7575
];
7676
$type = $options['bootstrap-type'];
7777
$size = $options['bootstrap-size'];
78-
unset($options['bootstrap-type'], $options['bootstrap-size']) ;
79-
$options = $this->addClass($options, 'btn') ;
80-
if (in_array($type, $this->buttonTypes)) {
81-
$options = $this->addClass($options, 'btn-'.$type) ;
78+
unset($options['bootstrap-type'], $options['bootstrap-size']);
79+
$options = $this->addClass($options, 'btn');
80+
if (!preg_match('#btn-[a-z]+#', $options['class'])) {
81+
$options = $this->addClass($options, 'btn-'.$type);
8282
}
83-
if (in_array($size, $this->buttonSizes)) {
84-
$options = $this->addClass($options, 'btn-'.$size) ;
83+
if ($size) {
84+
$options = $this->addClass($options, 'btn-'.$size);
8585
}
8686
return $options ;
8787
}

tests/TestCase/View/Helper/BootstrapFormHelperTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ public function testCreate () {
6363
$this->assertEquals($this->Form->inline, false) ;
6464
}
6565

66+
public function testButton () {
67+
// default button
68+
$button = $this->Form->button('Test');
69+
$this->assertHtml([
70+
['button' => [
71+
'class' => 'btn btn-default',
72+
'type' => 'submit'
73+
]], 'Test', '/button'
74+
], $button);
75+
// button with bootstrap-type and bootstrap-size
76+
$button = $this->Form->button('Test', [
77+
'bootstrap-type' => 'success',
78+
'bootstrap-size' => 'sm'
79+
]);
80+
$this->assertHtml([
81+
['button' => [
82+
'class' => 'btn btn-success btn-sm',
83+
'type' => 'submit'
84+
]], 'Test', '/button'
85+
], $button);
86+
// button with class
87+
$button = $this->Form->button('Test', [
88+
'class' => 'btn btn-primary'
89+
]);
90+
$this->assertHtml([
91+
['button' => [
92+
'class' => 'btn btn-primary',
93+
'type' => 'submit'
94+
]], 'Test', '/button'
95+
], $button);
96+
}
97+
6698
protected function _testInput ($expected, $fieldName, $options = []) {
6799
$formOptions = [] ;
68100
if (isset($options['_formOptions'])) {

0 commit comments

Comments
 (0)