From efc3959236ca0a008910ab42e6d7c3ac6eab2e9c Mon Sep 17 00:00:00 2001 From: Yasuo Harada Date: Thu, 27 Jun 2013 15:23:15 +0900 Subject: [PATCH] add checkboxDiv option and modified sample, test case --- .../View/Helper/BoostCakeFormHelperTest.php | 37 ++++++ View/BoostCake/bootstrap2.ctp | 90 ++++++------- View/BoostCake/bootstrap3.ctp | 118 +++++++++--------- View/Helper/BoostCakeFormHelper.php | 15 +++ 4 files changed, 162 insertions(+), 98 deletions(-) diff --git a/Test/Case/View/Helper/BoostCakeFormHelperTest.php b/Test/Case/View/Helper/BoostCakeFormHelperTest.php index 700c18d..4ecf79f 100644 --- a/Test/Case/View/Helper/BoostCakeFormHelperTest.php +++ b/Test/Case/View/Helper/BoostCakeFormHelperTest.php @@ -128,6 +128,43 @@ public function testBeforeInputAfterInput() { public function testCheckbox() { $result = $this->Form->input('name', array('type' => 'checkbox')); + $this->assertTags($result, array( + array('div' => array()), + array('div' => array('class' => 'input checkbox')), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'hidden', 'name' => 'data[name]', 'id' => 'name_', 'value' => '0')), + 'label' => array('for' => 'name'), + array('input' => array('name' => 'data[name]', 'type' => 'checkbox', 'value' => '1', 'id' => 'name')), + ' Name', + '/label', + '/div', + '/div', + '/div' + )); + + $result = $this->Form->input('name', array( + 'type' => 'checkbox', + 'before' => '', + 'label' => false + )); + $this->assertTags($result, array( + array('div' => array()), + array('label' => array()), + 'Name', + '/label', + array('div' => array('class' => 'input checkbox')), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'hidden', 'name' => 'data[name]', 'id' => 'name_', 'value' => '0')), + array('input' => array('name' => 'data[name]', 'type' => 'checkbox', 'value' => '1', 'id' => 'name')), + '/div', + '/div', + '/div' + )); + + $result = $this->Form->input('name', array( + 'type' => 'checkbox', + 'checkboxDiv' => false + )); $this->assertTags($result, array( array('div' => array()), array('div' => array('class' => 'input checkbox')), diff --git a/View/BoostCake/bootstrap2.ctp b/View/BoostCake/bootstrap2.ctp index 940af1c..803b4bb 100644 --- a/View/BoostCake/bootstrap2.ctp +++ b/View/BoostCake/bootstrap2.ctp @@ -35,10 +35,7 @@ 'after' => 'Example block-level help text here.' )); ?> Form->input('checkbox', array( - 'label' => array( - 'text' => 'Check me out', - 'class' => 'checkbox' - ) + 'label' => 'Check me out' )); ?> Form->submit('Submit', array( 'div' => false, @@ -63,10 +60,7 @@ echo h("Form->create('BoostCake', array( 'after' => 'Example block-level help text here.' )); ?> Form->input('checkbox', array( - 'label' => array( - 'text' => 'Check me out', - 'class' => 'checkbox' - ) + 'label' => 'Check me out' )); ?> Form->submit('Submit', array( 'div' => false, @@ -140,7 +134,8 @@ echo h("Form->create('BoostCake', array( 'label' => array( 'text' => 'Remember me', 'class' => 'checkbox' - ) + ), + 'checkboxDiv' => false )); ?> Form->submit('Sign in', array( 'div' => false, @@ -169,7 +164,8 @@ echo h("Form->create('BoostCake', array( 'label' => array( 'text' => 'Remember me', 'class' => 'checkbox' - ) + ), + 'checkboxDiv' => false )); ?> Form->submit('Sign in', array( 'div' => false, @@ -208,10 +204,7 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password' )); ?> Form->input('remember', array( - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ), + 'label' => 'Remember me', 'afterInput' => $this->Form->submit('Sign in', array( 'class' => 'btn' )) @@ -236,10 +229,7 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password' )); ?> Form->input('remember', array( - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ), + 'label' => 'Remember me', 'afterInput' => \$this->Form->submit('Sign in', array( 'class' => 'btn' )) @@ -296,18 +286,15 @@ echo h("Form->create('BoostCake', array( ) ) )); ?> -
- - Form->input('radio', array( - 'type' => 'radio', - 'div' => false, - 'legend' => false, - 'options' => array( - 1 => 'Option one is this and that—be sure to include why it\'s great', - 2 => 'Option two can be something else and selecting it will deselect option one' - ) - )); ?> -
+ Form->input('radio', array( + 'type' => 'radio', + 'before' => '', + 'legend' => false, + 'options' => array( + 1 => 'Option one is this and that—be sure to include why it\'s great', + 2 => 'Option two can be something else and selecting it will deselect option one' + ) + )); ?> Form->input('username', array( 'placeholder' => 'Username', 'div' => 'control-group input-prepend', @@ -338,6 +325,17 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password', 'errorMessage' => false )); ?> + Form->input('checkbox', array( + 'label' => array('class' => null), + 'afterInput' => 'Checkbox Bootstrap Style' + )); ?> + Form->input('checkbox', array( + 'div' => false, + 'label' => false, + 'before' => '', + 'wrapInput' => 'controls', + 'afterInput' => 'Checkbox CakePHP Style' + )); ?>
Form->submit('Save changes', array( 'div' => false, @@ -391,18 +389,15 @@ echo h("Form->create('BoostCake', array( ) ) )); ?> -
Form->isFieldError('radio')): ?> error\"> - - Form->input('radio', array( - 'type' => 'radio', - 'div' => false, - 'legend' => false, - 'options' => array( - 1 => 'Option one is this and that—be sure to include why it\'s great', - 2 => 'Option two can be something else and selecting it will deselect option one' - ) - )); ?> -
+ Form->input('radio', array( + 'type' => 'radio', + 'before' => '', + 'legend' => false, + 'options' => array( + 1 => 'Option one is this and that—be sure to include why it\'s great', + 2 => 'Option two can be something else and selecting it will deselect option one' + ) + )); ?> Form->input('username', array( 'placeholder' => 'Username', 'div' => 'control-group input-prepend', @@ -433,6 +428,17 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password', 'errorMessage' => false )); ?> + Form->input('checkbox', array( + 'label' => array('class' => null), + 'afterInput' => 'Checkbox Bootstrap Style' + )); ?> + Form->input('checkbox', array( + 'div' => false, + 'label' => false, + 'before' => '', + 'wrapInput' => 'controls', + 'afterInput' => 'Checkbox CakePHP Style' + )); ?>
Form->submit('Save changes', array( 'div' => false, diff --git a/View/BoostCake/bootstrap3.ctp b/View/BoostCake/bootstrap3.ctp index 97f65d0..475305b 100644 --- a/View/BoostCake/bootstrap3.ctp +++ b/View/BoostCake/bootstrap3.ctp @@ -35,10 +35,7 @@ 'after' => 'Example block-level help text here.' )); ?> Form->input('checkbox', array( - 'label' => array( - 'text' => 'Check me out', - 'class' => 'checkbox' - ) + 'label' => 'Check me out' )); ?> Form->submit('Submit', array( 'div' => false, @@ -63,10 +60,7 @@ echo h("Form->create('BoostCake', array( 'after' => 'Example block-level help text here.' )); ?> Form->input('checkbox', array( - 'label' => array( - 'text' => 'Check me out', - 'class' => 'checkbox' - ) + 'label' => 'Check me out' )); ?> Form->submit('Submit', array( 'div' => false, @@ -97,10 +91,7 @@ echo h("Form->create('BoostCake', array( 'style' => 'width:180px;' )); ?> Form->input('remember', array( - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ) + 'label' => 'Remember me' )); ?> Form->submit('Sign in', array( 'div' => false, @@ -126,10 +117,7 @@ echo h("Form->create('BoostCake', array( 'style' => 'width:180px;' )); ?> Form->input('remember', array( - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ) + 'label' => 'Remember me' )); ?> Form->submit('Sign in', array( 'div' => false, @@ -162,10 +150,7 @@ echo h("Form->create('BoostCake', array( )); ?> Form->input('remember', array( 'wrapInput' => 'col col-lg-10 col-offset-2', - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ), + 'label' => 'Remember me', 'afterInput' => $this->Form->submit('Sign in', array( 'class' => 'btn' )) @@ -191,10 +176,7 @@ echo h("Form->create('BoostCake', array( )); ?> Form->input('remember', array( 'wrapInput' => 'col col-lg-10 col-offset-2', - 'label' => array( - 'text' => 'Remember me', - 'class' => 'checkbox' - ), + 'label' => Remember me', 'afterInput' => \$this->Form->submit('Sign in', array( 'class' => 'btn' )) @@ -251,32 +233,33 @@ echo h("Form->create('BoostCake', array( ) ) )); ?> -
- - Form->input('radio', array( - 'type' => 'radio', - 'div' => false, - 'legend' => false, - 'options' => array( - 1 => 'Option one is this and that—be sure to include why it\'s great', - 2 => 'Option two can be something else and selecting it will deselect option one' - ) - )); ?> -
+ Form->input('radio', array( + 'type' => 'radio', + 'before' => '', + 'legend' => false, + 'options' => array( + 1 => 'Option one is this and that—be sure to include why it\'s great', + 2 => 'Option two can be something else and selecting it will deselect option one' + ) + )); ?> Form->input('username', array( 'placeholder' => 'Username', 'label' => array( 'text' => 'Prepend', ), - 'wrapInput' => 'input-group col col-lg-10', - 'beforeInput' => '@' + 'between' => '
', + 'wrapInput' => 'input-group', + 'beforeInput' => '@', + 'after' => '
' )); ?> Form->input('price', array( 'label' => array( 'text' => 'Append', ), - 'wrapInput' => 'input-group col col-lg-10', - 'afterInput' => '.00' + 'between' => '
', + 'wrapInput' => 'input-group', + 'afterInput' => '.00', + 'after' => '
' )); ?> Form->input('password', array( 'label' => array( @@ -293,6 +276,17 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password', 'errorMessage' => false )); ?> + Form->input('checkbox', array( + 'wrapInput' => 'col col-lg-10 col-offset-2', + 'label' => array('class' => null), + 'afterInput' => 'Checkbox Bootstrap Style' + )); ?> + Form->input('checkbox', array( + 'before' => '', + 'label' => false, + 'wrapInput' => 'col col-lg-10', + 'afterInput' => 'Checkbox CakePHP Style' + )); ?>
Form->submit('Save changes', array( @@ -348,32 +342,33 @@ echo h("Form->create('BoostCake', array( ) ) )); ?> -
Form->isFieldError('radio')): ?> error\"> - - Form->input('radio', array( - 'type' => 'radio', - 'div' => false, - 'legend' => false, - 'options' => array( - 1 => 'Option one is this and that—be sure to include why it\'s great', - 2 => 'Option two can be something else and selecting it will deselect option one' - ) - )); ?> -
+ Form->input('radio', array( + 'type' => 'radio', + 'before' => '', + 'legend' => false, + 'options' => array( + 1 => 'Option one is this and that—be sure to include why it\'s great', + 2 => 'Option two can be something else and selecting it will deselect option one' + ) + )); ?> Form->input('username', array( 'placeholder' => 'Username', 'label' => array( 'text' => 'Prepend', ), - 'wrapInput' => 'input-group col col-lg-10', - 'beforeInput' => '@' + 'between' => '
', + 'wrapInput' => 'input-group', + 'beforeInput' => '@', + 'after' => '
' )); ?> Form->input('price', array( 'label' => array( 'text' => 'Append', ), - 'wrapInput' => 'input-group col col-lg-10', - 'afterInput' => '.00' + 'between' => '
', + 'wrapInput' => 'input-group', + 'afterInput' => '.00', + 'after' => '
' )); ?> Form->input('password', array( 'label' => array( @@ -390,6 +385,17 @@ echo h("Form->create('BoostCake', array( 'placeholder' => 'Password', 'errorMessage' => false )); ?> + Form->input('checkbox', array( + 'wrapInput' => 'col col-lg-10 col-offset-2', + 'label' => array('class' => null), + 'afterInput' => 'Checkbox Bootstrap Style' + )); ?> + Form->input('checkbox', array( + 'before' => '', + 'label' => false, + 'wrapInput' => 'col col-lg-10', + 'afterInput' => 'Checkbox CakePHP Style' + )); ?>
Form->submit('Save changes', array( diff --git a/View/Helper/BoostCakeFormHelper.php b/View/Helper/BoostCakeFormHelper.php index 980b358..05e998d 100644 --- a/View/Helper/BoostCakeFormHelper.php +++ b/View/Helper/BoostCakeFormHelper.php @@ -48,6 +48,7 @@ class BoostCakeFormHelper extends FormHelper { * Added options * - `wrapInput` - Either `false` to disable the div wrapping input, or an array of options for the div. * See HtmlHelper::div() for more options. + * - `checkboxDiv` - Wrap input checkbox tag's class. * - `beforeInput` - Content to place before the input. * - `afterInput` - Content to place after the input. * - `errorClass` - Wrap input tag's error message class. @@ -70,6 +71,7 @@ public function input($fieldName, $options = array()) { 'wrapInput' => array( 'tag' => 'div' ), + 'checkboxDiv' => 'checkbox', 'beforeInput' => '', 'afterInput' => '', 'errorClass' => 'has-error error' @@ -87,6 +89,9 @@ public function input($fieldName, $options = array()) { if (isset($options['wrapInput'])) { unset($options['wrapInput']); } + if (isset($options['checkboxDiv'])) { + unset($options['checkboxDiv']); + } if (isset($options['beforeInput'])) { unset($options['beforeInput']); } @@ -105,6 +110,9 @@ public function input($fieldName, $options = array()) { $this->_inputDefaults = $inputDefaults; if ($this->_inputType === 'checkbox') { + if (isset($options['before'])) { + $html = str_replace($options['before'], '%before%', $html); + } $regex = '/()(.*?<\/label>)/'; if (preg_match($regex, $html, $label)) { $html = preg_replace($regex, '', $html); @@ -114,6 +122,9 @@ public function input($fieldName, $options = array()) { $html ); } + if (isset($options['before'])) { + $html = str_replace('%before%', $options['before'], $html); + } } return $html; @@ -155,6 +166,10 @@ protected function _divOptions($options) { */ protected function _getInput($args) { $input = parent::_getInput($args); + if ($this->_inputType === 'checkbox' && $this->_inputOptions['checkboxDiv'] !== false) { + $input = $this->Html->div($this->_inputOptions['checkboxDiv'], $input); + } + $beforeInput = $this->_inputOptions['beforeInput']; $afterInput = $this->_inputOptions['afterInput'];