CakePHP: Keep the same checkboxes checked after submit -
how can maintain same checkboxes checked after submit? other input fields on form automatically keeps values. thought go checkboxes, nope.
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 ));
i believe can done in controller, how?
edit:since posted question i've changed cakedcs search plugin, because i've gotten work before. still... can't work time.
adding model , controller code:
appcontroller public $components = array('debugkit.toolbar', 'session', 'auth' => array( 'loginaction' => '/', 'loginredirect' => '/login', 'logoutredirect' => '/', 'autherror' => 'du må logge inn å vise denne siden.', 'authorize' => array('controller'), ), 'search.prg' ); public $presetvars = true; //same in model filterargs(). 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 default filter when form 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 empty in sec array, first used. 'fields' => $this->ad->setfields(), 'limit' => 3 ); $this->set('res', $this->paginate()); }
model 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'])) { homecoming array(); } $str_filter = '%' . $data['search_field'] . '%'; homecoming array( 'or' => array( $this->alias . '.title like' => $str_filter, $this->alias . '.description like' => $str_filter, ) ); } /** * sets fields returned search. * * @access public * @return array database table fields * @author morten flydahl * */ public function setfields() { homecoming 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' ); }
you have set manually selected
alternative of input, array "keys = values = intval(checkbox id)"
i cannot explain why format, way work.
here code:
echo $this->form->create('user'); // read submitted value $selected = $this->form->value('user.albums'); // formats value if (empty($selected)) { $selected = array(); // avoid mess } else { $selected = array_map('intval', $selected); $selected = array_combine ($selected, $selected); } // renders 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, ... ) ));
hope helps. ++
cakephp submit checkbox
No comments:
Post a Comment