Browse Source

Removed the $value parameter from radio and checkbox.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
a4b7d493b0
  1. 30
      framework/helpers/base/Html.php
  2. 12
      tests/unit/framework/helpers/HtmlTest.php

30
framework/helpers/base/Html.php

@ -609,7 +609,6 @@ class Html
* Generates a radio button input. * Generates a radio button input.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param boolean $checked whether the radio button should be checked. * @param boolean $checked whether the radio button should be checked.
* @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute * - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute
@ -621,10 +620,10 @@ class Html
* *
* @return string the generated radio button tag * @return string the generated radio button tag
*/ */
public static function radio($name, $checked = false, $value = '1', $options = array()) public static function radio($name, $checked = false, $options = array())
{ {
$options['checked'] = $checked; $options['checked'] = $checked;
$options['value'] = $value; $value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) { if (isset($options['uncheck'])) {
// add a hidden field so that if the radio button is not selected, it still submits a value // add a hidden field so that if the radio button is not selected, it still submits a value
$hidden = static::hiddenInput($name, $options['uncheck']); $hidden = static::hiddenInput($name, $options['uncheck']);
@ -639,7 +638,6 @@ class Html
* Generates a checkbox input. * Generates a checkbox input.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param boolean $checked whether the checkbox should be checked. * @param boolean $checked whether the checkbox should be checked.
* @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute * - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute
@ -651,10 +649,10 @@ class Html
* *
* @return string the generated checkbox tag * @return string the generated checkbox tag
*/ */
public static function checkbox($name, $checked = false, $value = '1', $options = array()) public static function checkbox($name, $checked = false, $options = array())
{ {
$options['checked'] = $checked; $options['checked'] = $checked;
$options['value'] = $value; $value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) { if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value // add a hidden field so that if the checkbox is not selected, it still submits a value
$hidden = static::hiddenInput($name, $options['uncheck']); $hidden = static::hiddenInput($name, $options['uncheck']);
@ -806,7 +804,7 @@ class Html
if ($formatter !== null) { if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value); $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else { } else {
$lines[] = static::label(static::checkbox($name, $checked, $value) . ' ' . $label); $lines[] = static::label(static::checkbox($name, $checked, array('value' => $value)) . ' ' . $label);
} }
$index++; $index++;
} }
@ -860,7 +858,7 @@ class Html
if ($formatter !== null) { if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value); $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else { } else {
$lines[] = static::label(static::radio($name, $checked, $value) . ' ' . $label); $lines[] = static::label(static::radio($name, $checked, array('value' => $value)) . ' ' . $label);
} }
$index++; $index++;
} }
@ -1013,7 +1011,6 @@ class Html
* @param Model $model the model object * @param Model $model the model object
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
* about attribute expression. * about attribute expression.
* @param string $value the value tag attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set, * - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
@ -1026,17 +1023,17 @@ class Html
* *
* @return string the generated radio button tag * @return string the generated radio button tag
*/ */
public static function activeRadio($model, $attribute, $value = '1', $options = array()) public static function activeRadio($model, $attribute, $options = array())
{ {
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
$checked = static::getAttributeValue($model, $attribute); $checked = static::getAttributeValue($model, $attribute);
if (!array_key_exists('uncheck', $options)) { if (!array_key_exists('uncheck', $options)) {
$options['unchecked'] = '0'; $options['uncheck'] = '0';
} }
if (!array_key_exists('id', $options)) { if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute); $options['id'] = static::getInputId($model, $attribute);
} }
return static::radio($name, $checked, $value, $options); return static::radio($name, $checked, $options);
} }
/** /**
@ -1046,7 +1043,6 @@ class Html
* @param Model $model the model object * @param Model $model the model object
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
* about attribute expression. * about attribute expression.
* @param string $value the value tag attribute. If it is null, the value attribute will not be rendered.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set, * - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
@ -1059,17 +1055,17 @@ class Html
* *
* @return string the generated checkbox tag * @return string the generated checkbox tag
*/ */
public static function activeCheckbox($model, $attribute, $value = '1', $options = array()) public static function activeCheckbox($model, $attribute, $options = array())
{ {
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
$checked = static::getAttributeValue($model, $attribute); $checked = static::getAttributeValue($model, $attribute);
if (!array_key_exists('uncheck', $options)) { if (!array_key_exists('uncheck', $options)) {
$options['unchecked'] = '0'; $options['uncheck'] = '0';
} }
if (!array_key_exists('id', $options)) { if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute); $options['id'] = static::getInputId($model, $attribute);
} }
return static::checkbox($name, $checked, $value, $options); return static::checkbox($name, $checked, $options);
} }
/** /**
@ -1468,7 +1464,7 @@ class Html
*/ */
public static function getInputId($model, $attribute) public static function getInputId($model, $attribute)
{ {
$name = static::getInputName($model, $attribute); $name = strtolower(static::getInputName($model, $attribute));
return str_replace(array('[]', '][', '[', ']', ' '), array('', '-', '-', '', '-'), $name); return str_replace(array('[]', '][', '[', ']', ' '), array('', '-', '-', '', '-'), $name);
} }

12
tests/unit/framework/helpers/HtmlTest.php

@ -230,15 +230,15 @@ class HtmlTest extends \yii\test\TestCase
public function testRadio() public function testRadio()
{ {
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); $this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a'))); $this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, array('class' => 'a', 'value' => null)));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, array('class' => 'a' , 'uncheck' => '0', 'value' => 2)));
} }
public function testCheckbox() public function testCheckbox()
{ {
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); $this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a'))); $this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, array('class' => 'a', 'value' => null)));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, array('class' => 'a', 'uncheck' => '0', 'value' => 2)));
} }
public function testDropDownList() public function testDropDownList()
@ -347,7 +347,7 @@ EOD;
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, array('value' => $value)));
} }
))); )));
} }
@ -383,7 +383,7 @@ EOD;
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( $this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::radio($name, $checked, array('value' => $value)));
} }
))); )));
} }

Loading…
Cancel
Save