Created
July 19, 2012 17:29
-
-
Save henriqueboaventura/3145515 to your computer and use it in GitHub Desktop.
select from model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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