Skip to content

Commit

Permalink
Fixes yiisoft#9771: Assign hidden input with its own set of HTML opti…
Browse files Browse the repository at this point in the history
…ons via `$hiddenOptions` in activeFileInput `$options`
  • Loading branch information
HanafiAhmat authored and samdark committed Jan 24, 2018
1 parent 08eccb6 commit 4f7b410
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions framework/helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -1430,22 +1430,29 @@ public static function activePasswordInput($model, $attribute, $options = [])
* 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);
Expand Down
20 changes: 20 additions & 0 deletions tests/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,26 @@ public function testActiveFileInput()
$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);
}

/**
Expand Down

0 comments on commit 4f7b410

Please sign in to comment.