Browse Source

Fixes #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options`

tags/2.0.14
Hanafi Ahmat 7 years ago committed by Alexander Makarov
parent
commit
4f7b410230
  1. 1
      framework/CHANGELOG.md
  2. 11
      framework/helpers/BaseHtml.php
  3. 20
      tests/framework/helpers/HtmlTest.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.14 under development
------------------------
- Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat)
- Bug #15536: Fixed `yii\widgets\ActiveForm::init()` for call `parent::init()` (panchenkodv)
- Enh #14806: Added $placeFooterAfterBody option for GridView (terehru)
- Bug #14711: Fixed `yii\web\ErrorHandler` displaying exception message in non-debug mode (samdark)

11
framework/helpers/BaseHtml.php

@ -1430,22 +1430,29 @@ class BaseHtml
* Generates a file input tag for the given model attribute.
* This method will generate the "name" and "value" tag attributes automatically for the model attribute
* unless they are explicitly specified in `$options`.
* Additionally, if a separate set of HTML options array is defined inside `$options` with a key named `hiddenOptions`,
* it will be passed to the `activeHiddenInput` field as its own `$options` parameter.
* @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. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
* If `hiddenOptions` parameter which is another set of HTML options array is defined, it will be extracted
* from `$options` to be used for the hidden input.
* @return string the generated input tag
*/
public static function activeFileInput($model, $attribute, $options = [])
{
// add a hidden field so that if a model only has a file field, we can
// still use isset($_POST[$modelClass]) to detect if the input is submitted
$hiddenOptions = ['id' => null, 'value' => ''];
if (isset($options['name'])) {
$hiddenOptions['name'] = $options['name'];
}
$hiddenOptions = ArrayHelper::merge($hiddenOptions, ArrayHelper::remove($options, 'hiddenOptions', []));
// add a hidden field so that if a model only has a file field, we can
// still use isset($_POST[$modelClass]) to detect if the input is submitted.
// The hidden input will be assigned its own set of html options via `$hiddenOptions`.
// This provides the possibility to interact with the hidden field via client script.
return static::activeHiddenInput($model, $attribute, $hiddenOptions)
. static::activeInput('file', $model, $attribute, $options);

20
tests/framework/helpers/HtmlTest.php

@ -1467,6 +1467,26 @@ EOD;
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['name' => 'foo']);
$this->assertEqualsWithoutLE($expected, $actual);
$expected = '<input type="hidden" id="specific-id" name="foo" value=""><input type="file" id="htmltestmodel-types" name="foo">';
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['name' => 'foo', 'hiddenOptions'=>['id'=>'specific-id']]);
$this->assertEqualsWithoutLE($expected, $actual);
$expected = '<input type="hidden" id="specific-id" name="HtmlTestModel[types]" value=""><input type="file" id="htmltestmodel-types" name="HtmlTestModel[types]">';
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['hiddenOptions'=>['id'=>'specific-id']]);
$this->assertEqualsWithoutLE($expected, $actual);
$expected = '<input type="hidden" name="HtmlTestModel[types]" value=""><input type="file" id="htmltestmodel-types" name="HtmlTestModel[types]">';
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['hiddenOptions'=>[]]);
$this->assertEqualsWithoutLE($expected, $actual);
$expected = '<input type="hidden" name="foo" value=""><input type="file" id="htmltestmodel-types" name="foo">';
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['name' => 'foo', 'hiddenOptions'=>[]]);
$this->assertEqualsWithoutLE($expected, $actual);
}
/**

Loading…
Cancel
Save