Skip to content

Instantly share code, notes, and snippets.

@henriqueboaventura
Created July 19, 2012 17:29
Show Gist options
  • Save henriqueboaventura/3145515 to your computer and use it in GitHub Desktop.
Save henriqueboaventura/3145515 to your computer and use it in GitHub Desktop.
select from model
<?php
<?php defined('SYSPATH') or die('No direct script access.');
class Form extends Kohana_Form{
public static function select_from_model($name, array $collection, array $config = null , $selected = NULL, array $attributes = NULL)
{
$base_config = array(
'empty' => false,
'value' => 'id',
'text' => 'name'
);
$config = array_merge(
$base_config,
$config
);
if($config['empty']){
$options[''] = $config['empty'];
}
foreach ($collection as $option) {
$options[$option->$config['value']] = $option->$config['text'];
}
return Form::select($name, $options, $selected, $attributes);
}
} // End form
?>
<?php
echo Form::select_from_model(
'state_id',
State::all(),
array(
'empty' => 'selecione',
'text' => 'alias'
),
$benefit->state_id,
array(
'id' => 'benefit_state_id',
'class' => 'input-xlarge'
)
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment