mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +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
|
@ -545,7 +545,7 @@ class FormHelper extends AppHelper {
|
|||
}
|
||||
|
||||
if (!empty($div)) {
|
||||
$divOptions = array('class'=>'input');
|
||||
$divOptions = array('class' => 'input');
|
||||
if (is_string($div)) {
|
||||
$divOptions['class'] = $div;
|
||||
} elseif (is_array($div)) {
|
||||
|
@ -567,7 +567,7 @@ class FormHelper extends AppHelper {
|
|||
|
||||
if (in_array($options['type'], array('date', 'datetime'))) {
|
||||
$labelFor = $this->domId(implode('.', array_filter(array($this->model(), $this->field()))));
|
||||
$labelAttributes = array( 'for' => $labelFor . 'Month' );
|
||||
$labelAttributes = array('for' => $labelFor . 'Month');
|
||||
}
|
||||
|
||||
if (is_array($label)) {
|
||||
|
@ -975,7 +975,7 @@ class FormHelper extends AppHelper {
|
|||
}
|
||||
$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) {
|
||||
$showEmpty = '';
|
||||
}
|
||||
|
@ -1268,9 +1268,12 @@ class FormHelper extends AppHelper {
|
|||
* @return array
|
||||
*/
|
||||
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
|
||||
$attributes = am(array('escape' => true), $attributes);
|
||||
|
||||
$select = array();
|
||||
$attributes = am(array('escape' => true), $attributes);
|
||||
$selectedIsEmpty = ($selected === '' || $selected === null);
|
||||
$selectedIsArray = is_array($selected);
|
||||
|
||||
foreach ($elements as $name => $title) {
|
||||
$htmlOptions = array();
|
||||
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
||||
|
@ -1290,12 +1293,9 @@ class FormHelper extends AppHelper {
|
|||
unset($htmlOptions['name'], $htmlOptions['value']);
|
||||
}
|
||||
if ($name !== null) {
|
||||
if ($selected !== '' && ($selected !== null) && ($selected == $name)) {
|
||||
$htmlOptions['selected'] = 'selected';
|
||||
} elseif (is_array($selected) && in_array($name, $selected)) {
|
||||
if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) {
|
||||
$htmlOptions['selected'] = 'selected';
|
||||
}
|
||||
|
||||
if ($showParents || (!in_array($title, $parents))) {
|
||||
$title = ife($attributes['escape'], h($title), $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);
|
||||
$this->assertPattern('/' .
|
||||
'<select[^>]*>\s+' .
|
||||
'<select[^>]*>\s*' .
|
||||
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
||||
'<option\s+value="second"[^>]*>value<\/option>\s+'.
|
||||
'<\/select>'.
|
||||
'/i', $result);
|
||||
'<\/select>/i',
|
||||
$result);
|
||||
|
||||
$result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array('escape' => false), false);
|
||||
$this->assertPattern('/' .
|
||||
'<select[^>]*>\s+' .
|
||||
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
||||
'<option\s+value="second"[^>]*>value<\/option>\s+'.
|
||||
'<select[^>]*>\s*' .
|
||||
'<option[^<>\/]+value="first"[^>]*>first "html" <chars><\/option>\s+'.
|
||||
'<option[^<>\/]+value="second"[^>]*>value<\/option>\s+'.
|
||||
'<\/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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue