views:

32

answers:

1
<?php
// My controller.
$marcas = ORM::Factory('marca')->
find_all()->
as_array('nome', 'nome');
array_unshift($marcas, '-- Selecione --');
?>

<?php
// My view.
echo Form::select('marca', $marcas, '-- Selecione --')
?>

Is there a faster way to add a default option in a select? Thank you.

A: 

Your way looks pretty fast and elegant, using the existing framework functionality with some smart data to take advantage of it.

You could probably extend Form::select() with your own code if you want to have any customized behaviour not outright supported. I know Kohana strongly suggests extending its core classes, that but I haven't played with Kohana3 yet. In Kohana2 you'd do it as seen here. According to this tutorial for Kohana3 you'd do it similarly, but placing your new file in the application/classes folder.

A wild stab at guessing how this would work: create form.php in application/classes and enter:

class Form extends Form_Core {

    public static function select() {
        /**
         * Add the code from http://dev.kohanaframework.org/projects/kohana3-core/repository/revisions/master/entry/classes/kohana/form.php#L252
         * and change it slightly to also include a default value when writing out
         * the form, or even better via another optional function parameter
         */
    }
}
Fanis