From 348cc17dbba72767a6eeee7a3b183cf9df0fce35 Mon Sep 17 00:00:00 2001 From: Cesar Nicola Date: Tue, 19 Apr 2016 11:04:15 -0300 Subject: [PATCH] Fixes #11168: `yii\helpers\BaseHtml` now uses abstracted `booleanInput()` and `activeBooleanInput()` methods to render `radio()`, `checkbox()`, `activeRadio()` and `activeCheckbox()` --- framework/CHANGELOG.md | 8 ++- framework/helpers/BaseHtml.php | 132 ++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 88 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5b6b75b..82b8662 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -38,11 +38,17 @@ Yii Framework 2 Change Log - Bug #11536: Fixed regression introduced in 2.0.8, where scalar value was not allowed in QueryBuilder `IN` condition anymore (cebe) - Bug #11672: Fixed `yii\validators\NumberValidator` erroring when value is an object without `__toString()` method (SamMousa) - Bug #11561: Fixed DI container throwing exceptions for optional dependencies (SamMousa) - +- Enh #11168: `yii\helpers\BaseHtml` now uses abstracted `booleanInput()` and `activeBooleanInput()` methods to render `radio()`, `checkbox()`, `activeRadio()` and `activeCheckbox()` (cesarnicola) 2.0.8 April 28, 2016 -------------------- +- Enh #9425: `yii\db\Query::exists()` now uses SQL standard `EXISTS()` query via new `yii\db\QueryBuilder::selectExists()` method to improving performance in some cases (PowerGamer1) +- Bug #9935: Fixed `yii\validators\EachValidator` does not invoke `validateAttribute()` method of the embedded validator (klimov-paul) +- Bug #11270: Fixed `BaseActiveRecord::link()` method in order to support closure in `indexBy` for relations declaration (iushev) +- Bug #11333: Avoid serializing PHP 7 errors (zuozp8) +- Bug #11262: Enabled use of yii2 inside of PHAR packaged console applications (hiqsol) +- Bug #11196: Fixed VarDumper throws PHP Fatal when dumping `__PHP_Incomplete_Class` (DamianZ) - Bug #7627: Fixed `yii\widgets\ActiveField` to handle inputs AJAX validation with changed ID properly (dizeee) - Bug #7717: Fixed the bug that `$properties` parameter in `ArrayHelper::toArray()` was not passed to recursive calls (quantum13) - Bug #9074: Fixed JS call `$('#grid').yiiGridView('getSelectedRows')` when `GridView::$showHeader` is set to false (NekitoSP, silverfire) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index cc4d70e..e9500ea 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -672,48 +672,35 @@ class BaseHtml * Generates a radio button input. * @param string $name the name attribute. * @param boolean $checked whether the radio button should be checked. - * @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 - * is present, a hidden input will be generated so that if the radio button is not checked and is submitted, - * the value of this attribute will still be submitted to the server via the hidden input. - * - label: string, a label displayed next to the radio button. It will NOT be HTML-encoded. Therefore you can pass - * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. - * When this option is specified, the radio button will be enclosed by a label tag. - * - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. - * - * The rest of the options will be rendered as the attributes of the resulting radio button tag. The values will - * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. - * See [[renderTagAttributes()]] for details on how attributes are being rendered. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. * * @return string the generated radio button tag */ public static function radio($name, $checked = false, $options = []) { - $options['checked'] = (bool) $checked; - $value = array_key_exists('value', $options) ? $options['value'] : '1'; - if (isset($options['uncheck'])) { - // add a hidden field so that if the radio button is not selected, it still submits a value - $hidden = static::hiddenInput($name, $options['uncheck']); - unset($options['uncheck']); - } else { - $hidden = ''; - } - if (isset($options['label'])) { - $label = $options['label']; - $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; - unset($options['label'], $options['labelOptions']); - $content = static::label(static::input('radio', $name, $value, $options) . ' ' . $label, null, $labelOptions); - return $hidden . $content; - } else { - return $hidden . static::input('radio', $name, $value, $options); - } + return static::booleanInput('radio', $name, $checked, $options); } /** * Generates a checkbox input. * @param string $name the name attribute. * @param boolean $checked whether the checkbox should be checked. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. + * + * @return string the generated checkbox tag + */ + public static function checkbox($name, $checked = false, $options = []) + { + return static::booleanInput('checkbox', $name, $checked, $options); + } + + /** + * Generates a boolean input. + * @param string $type the input type. This can be either `radio` or `checkbox`. + * @param string $name the name attribute. + * @param boolean $checked whether the checkbox should be checked. * @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 @@ -729,8 +716,9 @@ class BaseHtml * See [[renderTagAttributes()]] for details on how attributes are being rendered. * * @return string the generated checkbox tag + * @since 2.0.9 */ - public static function checkbox($name, $checked = false, $options = []) + protected static function booleanInput($type, $name, $checked = false, $options = []) { $options['checked'] = (bool) $checked; $value = array_key_exists('value', $options) ? $options['value'] : '1'; @@ -745,10 +733,10 @@ class BaseHtml $label = $options['label']; $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; unset($options['label'], $options['labelOptions']); - $content = static::label(static::input('checkbox', $name, $value, $options) . ' ' . $label, null, $labelOptions); + $content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions); return $hidden . $content; } else { - return $hidden . static::input('checkbox', $name, $value, $options); + return $hidden . static::input($type, $name, $value, $options); } } @@ -1396,47 +1384,14 @@ class BaseHtml * @param Model $model the model object * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * about attribute expression. - * @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, - * it will take the default value '0'. This method will render a hidden input so that if the radio button - * is not checked and is submitted, the value of this attribute will still be submitted to the server - * via the hidden input. If you do not want any hidden input, you should explicitly set this option as null. - * - label: string, a label displayed next to the radio button. It will NOT be HTML-encoded. Therefore you can pass - * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. - * The radio button will be enclosed by the label tag. Note that if you do not specify this option, a default label - * will be used based on the attribute label declaration in the model. If you do not want any label, you should - * explicitly set this option as null. - * - labelOptions: array, the HTML attributes for the label tag. This is only used when the "label" option is specified. - * - * The rest of the options will be rendered as the attributes of the resulting tag. The values will - * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. - * See [[renderTagAttributes()]] for details on how attributes are being rendered. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. * * @return string the generated radio button tag */ public static function activeRadio($model, $attribute, $options = []) { - $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); - $value = static::getAttributeValue($model, $attribute); - - if (!array_key_exists('value', $options)) { - $options['value'] = '1'; - } - if (!array_key_exists('uncheck', $options)) { - $options['uncheck'] = '0'; - } - if (!array_key_exists('label', $options)) { - $options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute))); - } - - $checked = "$value" === "{$options['value']}"; - - if (!array_key_exists('id', $options)) { - $options['id'] = static::getInputId($model, $attribute); - } - - return static::radio($name, $checked, $options); + return static::activeBooleanInput('checkbox', $model, $attribute, $options); } /** @@ -1445,27 +1400,30 @@ class BaseHtml * @param Model $model the model object * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * about attribute expression. - * @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, - * it will take the default value '0'. This method will render a hidden input so that if the radio button - * is not checked and is submitted, the value of this attribute will still be submitted to the server - * via the hidden input. If you do not want any hidden input, you should explicitly set this option as null. - * - label: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass - * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. - * The checkbox will be enclosed by the label tag. Note that if you do not specify this option, a default label - * will be used based on the attribute label declaration in the model. If you do not want any label, you should - * explicitly set this option as null. - * - labelOptions: array, the HTML attributes for the label tag. This is only used when the "label" option is specified. - * - * The rest of the options will be rendered as the attributes of the resulting tag. The values will - * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. - * See [[renderTagAttributes()]] for details on how attributes are being rendered. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. * * @return string the generated checkbox tag */ public static function activeCheckbox($model, $attribute, $options = []) { + return static::activeBooleanInput('checkbox', $model, $attribute, $options); + } + + /** + * Generates a boolean input + * This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]]. + * @param string $type the input type. This can be either `radio` or `checkbox`. + * @param Model $model the model object + * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format + * about attribute expression. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. + * @return string the generated input element + * @since 2.0.9 + */ + protected static function activeBooleanInput($type, $model, $attribute, $options = []) + { $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); $value = static::getAttributeValue($model, $attribute); @@ -1485,7 +1443,7 @@ class BaseHtml $options['id'] = static::getInputId($model, $attribute); } - return static::checkbox($name, $checked, $options); + return static::$type($name, $checked, $options); } /**