Browse Source

Fix #18593: Fix setting the `maxlength` attribute for `Html::activeInput()` and `Html::activeTextArea()` based on `length` parameter of validator

bizley-patch-1
bscheshirwork 4 years ago committed by GitHub
parent
commit
5ad809e815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 22
      framework/helpers/BaseHtml.php
  3. 56
      tests/framework/helpers/HtmlTest.php

1
framework/CHANGELOG.md

@ -19,6 +19,7 @@ Yii Framework 2 Change Log
- Bug #17631: Fix `yii\widgets\BaseListView` to properly render custom summary (sjaakp, bizley) - Bug #17631: Fix `yii\widgets\BaseListView` to properly render custom summary (sjaakp, bizley)
- Bug #17203: Fix `yii\db\Connection` to persist customized `queryBuilder` configuration after the `close()``open()` cycle (silverfire) - Bug #17203: Fix `yii\db\Connection` to persist customized `queryBuilder` configuration after the `close()``open()` cycle (silverfire)
- Bug #18325: Fix `yii\db\pgsql\Schema` to respect non-default PgSQL schema name for data types (theonedemon, silverfire) - Bug #18325: Fix `yii\db\pgsql\Schema` to respect non-default PgSQL schema name for data types (theonedemon, silverfire)
- Bug #18593: Fix setting the `maxlength` attribute for `Html::activeInput()` and `Html::activeTextArea()` based on `length` parameter of validator (BSCheshir)
- Bug #18592: Fix `yii\db\Command::getRawSql()` to not replace query params in invalid places (sartor) - Bug #18592: Fix `yii\db\Command::getRawSql()` to not replace query params in invalid places (sartor)
2.0.41.1 March 04, 2021 2.0.41.1 March 04, 2021

22
framework/helpers/BaseHtml.php

@ -1373,7 +1373,8 @@ class BaseHtml
/** /**
* If `maxlength` option is set true and the model attribute is validated by a string validator, * If `maxlength` option is set true and the model attribute is validated by a string validator,
* the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. * the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] and
* [[\yii\validators\StringValidator::length]].
* @param Model $model the model object * @param Model $model the model object
* @param string $attribute the attribute name or expression. * @param string $attribute the attribute name or expression.
* @param array $options the tag options in terms of name-value pairs. * @param array $options the tag options in terms of name-value pairs.
@ -1384,8 +1385,8 @@ class BaseHtml
unset($options['maxlength']); unset($options['maxlength']);
$attrName = static::getAttributeName($attribute); $attrName = static::getAttributeName($attribute);
foreach ($model->getActiveValidators($attrName) as $validator) { foreach ($model->getActiveValidators($attrName) as $validator) {
if ($validator instanceof StringValidator && $validator->max !== null) { if ($validator instanceof StringValidator && ($validator->max !== null || $validator->length !== null)) {
$options['maxlength'] = $validator->max; $options['maxlength'] = max($validator->max, $validator->length);
break; break;
} }
} }
@ -1405,8 +1406,9 @@ class BaseHtml
* The following special options are recognized: * The following special options are recognized:
* *
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
* by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. * by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]]
* This is available since version 2.0.3. * and [[\yii\validators\StringValidator::length].
* This is available since version 2.0.3 and improved taking `length` into account since version 2.0.42.
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used * - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used
* as a placeholder (this behavior is available since version 2.0.14). * as a placeholder (this behavior is available since version 2.0.14).
* *
@ -1465,8 +1467,9 @@ class BaseHtml
* The following special options are recognized: * The following special options are recognized:
* *
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
* by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. * by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]]
* This option is available since version 2.0.6. * and [[\yii\validators\StringValidator::length].
* This is available since version 2.0.6 and improved taking `length` into account since version 2.0.42.
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used * - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used
* as a placeholder (this behavior is available since version 2.0.14). * as a placeholder (this behavior is available since version 2.0.14).
* *
@ -1526,8 +1529,9 @@ class BaseHtml
* The following special options are recognized: * The following special options are recognized:
* *
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
* by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. * by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]]
* This option is available since version 2.0.6. * and [[\yii\validators\StringValidator::length].
* This is available since version 2.0.6 and improved taking `length` into account since version 2.0.42.
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used * - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used
* as a placeholder (this behavior is available since version 2.0.14). * as a placeholder (this behavior is available since version 2.0.14).
* *

56
tests/framework/helpers/HtmlTest.php

@ -1357,6 +1357,56 @@ EOD;
} }
/** /**
* Data provider for [[testActiveTextInputMaxLength]].
* @return array test data
*/
public function dataProviderActiveTextInputMaxLength()
{
return [
[
'some text',
[],
'<input type="text" id="htmltestmodel-title" name="HtmlTestModel[title]" value="some text">',
'<input type="text" id="htmltestmodel-alias" name="HtmlTestModel[alias]" value="some text">',
],
[
'',
[
'maxlength' => true,
],
'<input type="text" id="htmltestmodel-title" name="HtmlTestModel[title]" value="" maxlength="10">',
'<input type="text" id="htmltestmodel-alias" name="HtmlTestModel[alias]" value="" maxlength="20">',
],
[
'',
[
'maxlength' => 99,
],
'<input type="text" id="htmltestmodel-title" name="HtmlTestModel[title]" value="" maxlength="99">',
'<input type="text" id="htmltestmodel-alias" name="HtmlTestModel[alias]" value="" maxlength="99">',
],
];
}
/**
* @dataProvider dataProviderActiveTextInputMaxLength
*
* @param string $value
* @param array $options
* @param string $expectedHtmlForTitle
* @param string $expectedHtmlForAlias
*/
public function testActiveTextInputMaxLength($value, array $options, $expectedHtmlForTitle, $expectedHtmlForAlias)
{
$model = new HtmlTestModel();
$model->title = $value;
$model->alias = $value;
$this->assertEquals($expectedHtmlForTitle, Html::activeInput('text', $model, 'title', $options));
$this->assertEquals($expectedHtmlForAlias, Html::activeInput('text', $model, 'alias', $options));
}
/**
* Data provider for [[testActivePasswordInput()]]. * Data provider for [[testActivePasswordInput()]].
* @return array test data * @return array test data
*/ */
@ -2052,6 +2102,8 @@ class MyHtml extends Html{
/** /**
* @property string name * @property string name
* @property string title
* @property string alias
* @property array types * @property array types
* @property string description * @property string description
*/ */
@ -2059,7 +2111,7 @@ class HtmlTestModel extends DynamicModel
{ {
public function init() public function init()
{ {
foreach (['name', 'types', 'description', 'radio', 'checkbox'] as $attribute) { foreach (['name', 'title', 'alias', 'types', 'description', 'radio', 'checkbox'] as $attribute) {
$this->defineAttribute($attribute); $this->defineAttribute($attribute);
} }
} }
@ -2069,6 +2121,8 @@ class HtmlTestModel extends DynamicModel
return [ return [
['name', 'required'], ['name', 'required'],
['name', 'string', 'max' => 100], ['name', 'string', 'max' => 100],
['title', 'string', 'length' => 10],
['alias', 'string', 'length' => [0, 20]],
['description', 'string', 'max' => 500], ['description', 'string', 'max' => 500],
[['radio', 'checkbox'], 'boolean'], [['radio', 'checkbox'], 'boolean'],
]; ];

Loading…
Cancel
Save