public function test_choices() { $blogs = Jam::all('test_blog')->load_fields(array(array('id' => 1, 'name' => 'Flowers blog', 'url' => 'http://flowers.wordpress.com'), array('id' => 2, 'name' => 'Awesome programming', 'url' => 'http://programming-blog.com'), array('id' => 3, 'name' => 'Tabless', 'url' => 'http://bobby-tables-ftw.com'))); $select = $blogs->as_array(':primary_key', ':name_key'); $choices = Jam_Form::list_choices($blogs); $this->assertEquals($select, $choices); $choices = Jam_Form::list_choices($select); $this->assertEquals($select, $choices); }
/** * Radios select, bootstrap style * @param string $name * @param array $options * @param array $attributes * @return string */ public function radios($name, array $options = array(), array $attributes = array()) { $attributes = $this->default_attributes($name, $attributes); if (!isset($options['choices'])) { throw new Kohana_Exception('Radios tag widget requires a \'choices\' option'); } $choices = Jam_Form::list_choices($options['choices']); if ($blank = Arr::get($options, 'include_blank')) { Arr::unshift($choices, '', $blank === TRUE ? __(" -- Select -- ") : $blank); } $radios = array(); foreach ($choices as $key => $title) { $radio = $this->radio($name, array('value' => $key), array('id' => $attributes['id'] . '_' . $key)); $radios[] = new Builder_Html('label', array('class' => 'radio'), $radio . $title); } return '<div ' . HTML::attributes($attributes) . '>' . join("\n", $radios) . '</div>'; }
/** * HTML input select field * available options * - choices - this can be Jam_Query_Builder_Collection or a simple array * - include_blank - bool|string - include an empty option * * @param string $name the name of the Jam_Model attribute * @param array $options set the options of the select with 'choices' and 'include_blank' * @param array $attributes HTML attributes for the field * @return string */ public function select($name, array $options = array(), array $attributes = array()) { $attributes = $this->default_attributes($name, $attributes); if (!isset($options['choices'])) { throw new Kohana_Exception("Select tag widget requires a 'choices' option"); } $choices = Jam_Form::list_choices($options['choices']); if ($blank = Arr::get($options, 'include_blank')) { Arr::unshift($choices, '', $blank === TRUE ? " -- Select -- " : $blank); } $selected = Jam_Form::list_id($this->object()->{$name}); return Form::select($attributes['name'], $choices, $selected, $attributes); }