Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created September 15, 2010 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/580896 to your computer and use it in GitHub Desktop.
Save iloveitaly/580896 to your computer and use it in GitHub Desktop.
Formo 1.1.x multiple select driver
<?php defined('SYSPATH') or die('No direct script access.');
Formo::include_file('driver', 'select');
class Formo_mselect_Driver extends Formo_select_Driver {
public $selected_values = array();
public function __construct($name='',$info=array()) {
parent::__construct($name,$info);
if(count($this->selected_values) == 0 && !empty($this->value)) {
$this->selected_values = array($this->value);
}
}
public function add_post() {
$form = Formo::instance($this->formo_name);
$type = $form->_post_type;
$strippedName = substr($this->name, 0, strlen($this->name) - 2); // strip off the []
$this->selected_values = Input::instance()->$type($strippedName);
if(empty($this->selected_values)) {
$this->selected_values = array();
}
}
public function render() {
$sel= '<select name="'.$this->name.'"'.Formo::quicktagss($this->_find_tags())." multiple=\"multiple\">"."\n";
foreach ($this->values as $k=>$v) {
$k = preg_replace('/_[bB][lL][aA][nN][kK][0-9]*_/','',$k);
$selected = in_array($v, $this->selected_values) ? " selected='selected'" : '';
$sel .= "\t\t".'<option value="'.$v.'"'.$selected.'>'.$k.'</option>'."\n";
}
$sel.= "</select>";
return $sel;
}
protected function validate_this() {
if(empty($this->selected_values)) {
return $this->error = $this->required_msg;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment