Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic features and attributes are working, next up is making checked property smarter.
- Loading branch information
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\View\Input; | ||
|
||
use Cake\View\StringTemplate; | ||
|
||
/** | ||
* Input widget for creating checkbox widgets. | ||
*/ | ||
class Checkbox { | ||
|
||
/** | ||
* Template instance. | ||
* | ||
* @var Cake\View\StringTemplate | ||
*/ | ||
protected $_templates; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Cake\View\StringTemplate $templates | ||
*/ | ||
public function __construct($templates) { | ||
$this->_templates = $templates; | ||
} | ||
|
||
/** | ||
* Render a checkbox element. | ||
* | ||
* @param array $data The data to create a checkbox with. | ||
*/ | ||
public function render($data) { | ||
$data += [ | ||
'name' => '', | ||
'value' => 1, | ||
'checked' => false, | ||
'disabled' => false, | ||
]; | ||
$attrs = $this->_templates->formatAttributes( | ||
$data, | ||
['name', 'value'] | ||
); | ||
|
||
return $this->_templates->format('checkbox', [ | ||
'name' => $data['name'], | ||
'value' => $data['value'], | ||
'attrs' => $attrs | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Test\TestCase\View\Input; | ||
|
||
use Cake\TestSuite\TestCase; | ||
use Cake\View\Input\Checkbox; | ||
use Cake\View\StringTemplate; | ||
|
||
/** | ||
* Checkbox test case | ||
*/ | ||
class CheckboxTest extends TestCase { | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$templates = [ | ||
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>', | ||
]; | ||
$this->templates = new StringTemplate(); | ||
$this->templates->add($templates); | ||
} | ||
|
||
/** | ||
* Test rendering simple checkboxes. | ||
* | ||
* @return void | ||
*/ | ||
public function testRenderSimple() { | ||
$checkbox = new Checkbox($this->templates); | ||
$data = [ | ||
'name' => 'Comment[spam]', | ||
]; | ||
$result = $checkbox->render($data); | ||
$expected = [ | ||
'input' => [ | ||
'type' => 'checkbox', | ||
'name' => 'Comment[spam]', | ||
'value' => 1, | ||
] | ||
]; | ||
$this->assertTags($result, $expected); | ||
|
||
$data = [ | ||
'name' => 'Comment[spam]', | ||
'value' => 99 | ||
]; | ||
$result = $checkbox->render($data); | ||
$expected = [ | ||
'input' => [ | ||
'type' => 'checkbox', | ||
'name' => 'Comment[spam]', | ||
'value' => 99, | ||
] | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
/** | ||
* Test rendering disabled checkboxes. | ||
* | ||
* @return void | ||
*/ | ||
public function testRenderDisabled() { | ||
$checkbox = new Checkbox($this->templates); | ||
$data = [ | ||
'name' => 'Comment[spam]', | ||
'disabled' => true, | ||
]; | ||
$result = $checkbox->render($data); | ||
$expected = [ | ||
'input' => [ | ||
'type' => 'checkbox', | ||
'name' => 'Comment[spam]', | ||
'value' => 1, | ||
'disabled' => 'disabled', | ||
] | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
/** | ||
* Test rendering checked checkboxes. | ||
* | ||
* @return void | ||
*/ | ||
public function testRenderChecked() { | ||
$checkbox = new Checkbox($this->templates); | ||
$data = [ | ||
'name' => 'Comment[spam]', | ||
'value' => 1, | ||
'checked' => 1, | ||
]; | ||
$result = $checkbox->render($data); | ||
$expected = [ | ||
'input' => [ | ||
'type' => 'checkbox', | ||
'name' => 'Comment[spam]', | ||
'value' => 1, | ||
'checked' => 'checked', | ||
] | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
} |