LoginSignup
4
5

More than 5 years have passed since last update.

FuelPHP で複数選択させるチェックボックスを作る凡例

Posted at

View

<div class="control-group">
    <?= Form::label('Items', 'item_id[]', array('class'=>'control-label')); ?>

    <div class="controls">

<?php
    foreach ( Model_Item::find('all') as $item)
    {
        $checked = in_array($item->id, array_merge(
            (array) Input::post('item_id', []),
            isset($parent) ? array_keys($parent->items) : []
        ));
        echo Form::label(
            Form::checkbox(
                'item_id[]',
                $item->id,
                $checked,
                ['id' => 'form_item_id_' . $item->id]
            ) . $item->name,
            'item_id_' . $item->id,
            ['class' => 'checkbox']
        );
    }
?>

    </div>
</div>

Controller

<?php

class Controller_Parent extends Controller_Template
{

    /* ... 中略 ... */

    protected function apply_item_ids($parent)
    {
        $item_ids = (array) Input::post('item_id', []);

        foreach ( $parent->items as $item )
        {
            unset($parent->items[$item->id]);
        }

        foreach ( $item_ids as $item_id )
        {
            $parent->items[] = Model_Item::find($item_id);
        }
    }
}

改行入れたけど読みにくい罠。

あまり数の多いものには使えない。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5