2

I have a multidimensional array value for form attribute that I need to be included with form as hidden inputs.

$model->ids = ['first' => [1, 2], 'second' => [22]];

I can't use activeHiddenInput as it gives error

// gives error: "Array to string conversion"
<?= Html::activeHiddenInput($model, 'ids')?>

Expected outcome:

<input type="hidden" name="formName[ids][first][]" value="1" />
<input type="hidden" name="formName[ids][first][]" value="2" />
<input type="hidden" name="formName[ids][second][]" value="22" />

.. or ..

<input type="hidden" name="formName[ids][first][0]" value="1" />
<input type="hidden" name="formName[ids][first][1]" value="2" />
<input type="hidden" name="formName[ids][second][0]" value="22" />

What would be the best approach to solve this in yii2 framework concept?

4
  • loop through the field Commented May 22, 2017 at 9:36
  • I'm looking for good, generic solution that would work on any-depth arrays. Currently it would need 2 nested for loops, but what if there is depth of 3, 4? Commented May 22, 2017 at 9:52
  • write a recursive function than if the depth is not known/unlimited Commented May 22, 2017 at 10:05
  • eventually it will need to come to recursive function. But is there a good solution in yii2 concept? Where should this function be in order to be easy to use? Any tools that yii2 provides that would make it more readable, simple? Commented May 22, 2017 at 10:59

1 Answer 1

1

So this is how I solved with in case anyone needs it.

I extended yii/bootstrap/Html class with following methods:

/**
 * Generates list of hidden input tags for the given model attribute when the attribute's value is an array.
 *
 * @param Model $model
 * @param string $attribute
 * @param array $options
 * @return string
 */
public static function activeHiddenInputList($model, $attribute, $options = [])
{
    $str = '';
    $flattenedList = static::getflatInputNames($attribute, $model->$attribute);
    foreach ($flattenedList as $flattenAttribute) {
        $str.= static::activeHiddenInput($model, $flattenAttribute, $options);
    }
    return $str;
}

/**
 * @param string $name
 * @param array $values
 * @return array
 */
private static function getflatInputNames($name, array $values)
{
    $flattened = [];
    foreach ($values as $key => $val) {
        $nameWithKey = $name . '[' . $key . ']';
        if (is_array($val)) {
            $flattened += static::getflatInputNames($nameWithKey, $val);
        } else {
            $flattened[] = $nameWithKey;
        }
    }
    return $flattened;
}

Calling Html::activeHiddenInputList($model, 'ids'); will give output of

<input id="formname-ids-first-0" type="hidden" name="formName[ids][first][0]" value="1" />
<input id="formname-ids-first-1" type="hidden" name="formName[ids][first][1]" value="2" />
<input id="formname-ids-second-0" type="hidden" name="formName[ids][second][0]" value="22" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.