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

问题描述 投票:0回答:1

提交后如何保持相同的复选框处于选中状态?表单上的所有其他输入字段都会自动保留值。我以为这也适用于复选框,但事实并非如此。

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 搜索插件,因为我之前已经让它可以使用了。不过...这次我还是无法让它发挥作用。

添加模型和控制器代码:

应用程序控制器

 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.

广告控制器

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'
    );
}
php cakephp checkbox submit
1个回答
1
投票

您必须手动设置输入的

selected
选项,作为带有“keys = value = 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, ... )
));

希望这有帮助。 ++

© www.soinside.com 2019 - 2024. All rights reserved.