CakePHP:保持提交后选中相同的复选框

时间:2013-02-16 17:18:46

标签: cakephp submit checkbox

如何在提交后选中相同的复选框?表单上的所有其他输入字段自动保留值。我认为这也适用于复选框,但不是。

echo $this->Form->input('type_id', array(
    'multiple' => 'checkbox',
    'options' => array(
        '1' => 'Til salgs',
        '2' => 'Ønskes kjøpt',
        '3' => 'Gis bort'
    ),
    'div' => false,
    'label' => false
));

我相信这可以在控制器中完成,但是怎么做?

编辑:

自从我发布这个问题以来,我已经改为CakeDcs搜索插件,因为我之前已经开始使用它了。还是......这次我不能让它上班。

添加模型和控制器代码:

AppController的

 public $components = array('DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginAction' => '/',
        'loginRedirect' => '/login',
        'logoutRedirect' => '/',
        'authError' => 'Du må logge inn for å vise denne siden.',
        'authorize' => array('Controller'),
    ),
    'Search.Prg'
);
public $presetVars = true; //Same as in model filterArgs(). For Search-plugin.

AdsController

public function view() {
    $this->set('title_for_layout', 'Localtrade Norway');
    $this->set('show_searchbar', true); //Shows searchbar div in view
    $this->log($this->request->data, 'debug');

    //Setting users home commune as default filter when the form is not submitted.
    $default_filter = array(
        'Ad.commune_id' => $this->Auth->user('User.commune_id')
    );

    $this->Prg->commonProcess(); //Search-plugin
    $this->paginate = array(
        'conditions' => array_merge($default_filter, $this->Ad->parseCriteria($this->passedArgs)), //If Ad.commune_id is empty in second array, then the first will be used.
        'fields' => $this->Ad->setFields(),
        'limit' => 3
    );
    $this->set('res', $this->paginate());
}

模型

public $actsAs = array('Search.Searchable');
public $filterArgs = array(
    'search_field' => array('type' => 'query', 'method' => 'filterSearchField'),
    'commune_id' => array('type' => 'value'),
    'type_id' => array('type' => 'int')
);

public function filterSearchField($data) {
    if (empty($data['search_field'])) {
        return array();
    }
    $str_filter = '%' . $data['search_field'] . '%';
    return array(
        'OR' => array(
            $this->alias . '.title LIKE' => $str_filter,
            $this->alias . '.description LIKE' => $str_filter,
        )
    );
}

/**
 * Sets the fields which will be returned by the search.
 * 
 * @access public
 * @return array Database table fields
 * @author Morten Flydahl
 * 
 */
public function setFields() {
    return array(
        'Ad.id',
        'Ad.title',
        'Ad.description',
        'Ad.price',
        'Ad.modified',
        'User.id',
        'User.first_name',
        'User.middle_name',
        'User.last_name',
        'User.link',
        'User.picture_url',
        'Commune.name',
        'Type.id',
        'Type.name'
    );
}

1 个答案:

答案 0 :(得分:0)

您必须手动设置输入的selected选项,作为带有“ keys = values = intval(checkbox id)”的数组

我无法解释为什么这种格式,但这是我开始工作的唯一方法。

这是我的代码:

echo $this->Form->create('User');

// Read the submitted value
$selected = $this->Form->value('User.Albums');

// Formats the value
if (empty($selected)) {
  $selected = array(); // avoid mess
} else {
  $selected = array_map('intval', $selected);
  $selected = array_combine ($selected, $selected);
}

// Renders the checkboxes
echo $this->Form->input('Albums',array(
  'type' => 'select',
  'multiple' => 'checkbox',
  'options' => $albums, // array ( (int)id => string(label), ... )
  'selected' => $selected, // array ( (int)id => (int)id, ... )
));

希望这会有所帮助。 ++