Skip to content

Commit

Permalink
Merge pull request #5 from phillipsdata/set-select-options-between-op…
Browse files Browse the repository at this point in the history
…tgroups

Updated select form options to support closing an optgroup.
Fixes #4
  • Loading branch information
tysonphillips committed Jul 19, 2018
2 parents 58cedb4 + d10540d commit 6811915
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
22 changes: 14 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
dist: trusty
matrix:
include:
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2
- php: hhvm
before_script:
- composer install --dev
script:
- ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml
- ./vendor/bin/phpcs --extensions=php --report=summary --standard=PSR2 ./src ./tests
after_script:
- php ./vendor/bin/coveralls -v
- php ./vendor/bin/coveralls -v

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require-dev": {
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.2",
"satooshi/php-coveralls": "dev-master"
"php-coveralls/php-coveralls": "~1.0"
},
"autoload": {
"psr-4": {"Minphp\\Form\\": "src"}
Expand Down
36 changes: 28 additions & 8 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,11 @@ public function fieldFile($name, $attributes = array())
* Creates a select list
*
* @param string $name The name to set in the HTML name field
* @param array $options The options to place in this select list
* @param array $options A list of 'value'=>'name' options, or an array containing:
* - name The option label
* If set to 'optgroup', an <optgroup> tag is created
* If set to 'close_optgroup', an </optgroup> tag is created to close a previously opened optgroup
* - value The option value
* @param mixed $selected_value The option(s) to set as selected
* @param array $attributes Attributes for this input field
* @param array $option_attributes Attributes for each option to set. If
Expand Down Expand Up @@ -511,8 +515,12 @@ public function fieldSelect(
* Creates a select list with multiple selectable options
*
* @param string $name The name to set in the HTML name field
* @param array $options The options to place in this select list
* @param string $selected_value The option to set as selected
* @param array $options A list of 'value'=>'name' options, or an array containing:
* - name The option label
* If set to 'optgroup', an <optgroup> tag is created
* If set to 'close_optgroup', an </optgroup> tag is created to close a previously opened optgroup
* - value The option value
* @param string $selected_values The option to set as selected
* @param array $attributes Attributes for this input field
* @param array $option_attributes Attributes for each option to set. If
* single dimension will set the attributes for every option, if
Expand Down Expand Up @@ -594,10 +602,14 @@ private function fieldInput($tag, $type, $name, $value = null, $attributes = arr

/**
* Builds select options and optgroups if given. An optgroup will continue
* until the end of the select options, or until the next optgroup begins.
* until the end of the select options, until the next 'close_optgroup',
* or until the next optgroup begins.
*
* @param array $options A list of 'name'=>'value' options to set into
* <option> tags, if 'name' is 'optgroup' an <optgroup> tag is created instead
* @param array $options A list of 'value'=>'name' options, or an array containing:
* - name The option label
* If set to 'optgroup', an <optgroup> tag is created
* If set to 'close_optgroup', an </optgroup> tag is created to close a previously opened optgroup
* - value The option value
* @param array $selected_values Values corresponding to an option's value to set as selected
* @param array $attributes Attributes for each option field
* @return string The option fields along with any optgroups
Expand Down Expand Up @@ -631,9 +643,17 @@ private function selectOptions($options, $selected_values = array(), $attributes
$attr = $attributes[$value];
}

// Close an open optgroup tag if this option is set to do so, then move on
if (strpos($value, "close_optgroup") === 0 && $open_group_tag) {
$html .= "</optgroup>" . $this->eol;
$open_group_tag = false;
continue;
}

if (strpos($value, "optgroup") === 0) {
// Close an open optgroup tag before starting another
if ($open_group_tag) {
$html .= "</optgroup>" . $this->eol; //close an open tag before starting another
$html .= "</optgroup>" . $this->eol;
}

$html .= "<optgroup label=\"" . $this->_($name, true) . "\">";
Expand All @@ -652,7 +672,7 @@ private function selectOptions($options, $selected_values = array(), $attributes
}
}

//Before returning add closing optgroup tag if necessary
// Before returning, close the remaining optgroup tag if necessary
if ($open_group_tag) {
$html .= "</optgroup>" . $this->eol;
}
Expand Down

0 comments on commit 6811915

Please sign in to comment.