mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Refactoring FormHelper::__selectOptions() (Ticket #1312) and adding tests for multiple select boxes
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5516 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
e5de1e7f2b
commit
8d2d9d8684
2 changed files with 42 additions and 15 deletions
|
@ -975,7 +975,7 @@ class FormHelper extends AppHelper {
|
||||||
}
|
}
|
||||||
$select[] = sprintf($tag, $this->model(), $this->field(), $this->_parseAttributes($attributes));
|
$select[] = sprintf($tag, $this->model(), $this->field(), $this->_parseAttributes($attributes));
|
||||||
|
|
||||||
if ($showEmpty !== null && $showEmpty !== false) {
|
if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) {
|
||||||
if ($showEmpty === true) {
|
if ($showEmpty === true) {
|
||||||
$showEmpty = '';
|
$showEmpty = '';
|
||||||
}
|
}
|
||||||
|
@ -1268,9 +1268,12 @@ class FormHelper extends AppHelper {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
|
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
|
||||||
$attributes = am(array('escape' => true), $attributes);
|
|
||||||
|
|
||||||
$select = array();
|
$select = array();
|
||||||
|
$attributes = am(array('escape' => true), $attributes);
|
||||||
|
$selectedIsEmpty = ($selected === '' || $selected === null);
|
||||||
|
$selectedIsArray = is_array($selected);
|
||||||
|
|
||||||
foreach ($elements as $name => $title) {
|
foreach ($elements as $name => $title) {
|
||||||
$htmlOptions = array();
|
$htmlOptions = array();
|
||||||
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
||||||
|
@ -1290,12 +1293,9 @@ class FormHelper extends AppHelper {
|
||||||
unset($htmlOptions['name'], $htmlOptions['value']);
|
unset($htmlOptions['name'], $htmlOptions['value']);
|
||||||
}
|
}
|
||||||
if ($name !== null) {
|
if ($name !== null) {
|
||||||
if ($selected !== '' && ($selected !== null) && ($selected == $name)) {
|
if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) {
|
||||||
$htmlOptions['selected'] = 'selected';
|
|
||||||
} elseif (is_array($selected) && in_array($name, $selected)) {
|
|
||||||
$htmlOptions['selected'] = 'selected';
|
$htmlOptions['selected'] = 'selected';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($showParents || (!in_array($title, $parents))) {
|
if ($showParents || (!in_array($title, $parents))) {
|
||||||
$title = ife($attributes['escape'], h($title), $title);
|
$title = ife($attributes['escape'], h($title), $title);
|
||||||
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
|
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
|
||||||
|
|
|
@ -480,19 +480,46 @@ class FormHelperTest extends CakeTestCase {
|
||||||
|
|
||||||
$result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array(), false);
|
$result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array(), false);
|
||||||
$this->assertPattern('/' .
|
$this->assertPattern('/' .
|
||||||
'<select[^>]*>\s+' .
|
'<select[^>]*>\s*' .
|
||||||
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
||||||
'<option\s+value="second"[^>]*>value<\/option>\s+'.
|
'<option\s+value="second"[^>]*>value<\/option>\s+'.
|
||||||
'<\/select>'.
|
'<\/select>/i',
|
||||||
'/i', $result);
|
$result);
|
||||||
|
|
||||||
$result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array('escape' => false), false);
|
$result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array('escape' => false), false);
|
||||||
$this->assertPattern('/' .
|
$this->assertPattern('/' .
|
||||||
'<select[^>]*>\s+' .
|
'<select[^>]*>\s*' .
|
||||||
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
'<option[^<>\/]+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
||||||
'<option\s+value="second"[^>]*>value<\/option>\s+'.
|
'<option[^<>\/]+value="second"[^>]*>value<\/option>\s+'.
|
||||||
'<\/select>'.
|
'<\/select>'.
|
||||||
'/i', $result);
|
'/i',
|
||||||
|
$result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testSelectMultiple() {
|
||||||
|
$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => true));
|
||||||
|
$this->assertPattern('/^<select[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>\/]*>/', $result);
|
||||||
|
$this->assertPattern('/^<select[^<>]+id="ModelMultiField"[^<>\/]*>/', $result);
|
||||||
|
$this->assertPattern('/^<select[^<>]+multiple="multiple"[^<>\/]*>/', $result);
|
||||||
|
$this->assertNoPattern('/^<select[^<>]+[^name|id|multiple]=[^<>\/]*>/', $result);
|
||||||
|
|
||||||
|
$this->assertNoPattern('/option value=""/', $result);
|
||||||
|
$this->assertNoPattern('/selected/', $result);
|
||||||
|
$this->assertPattern('/<option[^<>]+value="0">first/', $result);
|
||||||
|
$this->assertPattern('/<option[^<>]+value="1">second/', $result);
|
||||||
|
$this->assertPattern('/<option[^<>]+value="2">third/', $result);
|
||||||
|
$this->assertNoPattern('/<option[^<>]+value="[^012]"[^<>\/]*>/', $result);
|
||||||
|
$this->assertPattern('/<\/select>$/', $result);
|
||||||
|
|
||||||
|
$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => 'multiple'));
|
||||||
|
$this->assertPattern('/^<select[^<>]+multiple="multiple"[^<>\/]*>/', $result);
|
||||||
|
$this->assertNoPattern('/^<select[^<>]+[^name|id|multiple]=[^<>\/]*>/', $result);
|
||||||
|
|
||||||
|
$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => true));
|
||||||
|
$this->assertPattern('/<option[^<>]+value="0"[^<>]+selected="selected">first/', $result);
|
||||||
|
$this->assertPattern('/<option[^<>]+value="1"[^<>]+selected="selected">second/', $result);
|
||||||
|
$this->assertPattern('/<option[^<>]+value="2">third/', $result);
|
||||||
|
$this->assertNoPattern('/<option[^<>]+value="[^012]"[^<>\/]*>/', $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCheckbox() {
|
function testCheckbox() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue