gshank/html-formhandler

allow ::Field::Select to find its values from a database enum definition

karenetheridge opened this issue · 0 comments

I'm doing this in CE code, and this would be a lot nicer if the field class itself knew how to do this (probably in HTML::FormHandler::Model::DBIC I suppose):

package CE::Form::Admin::Scholarship::Basics;
...
has_field 'award_type' => (
    required     => 1,
    label        => 'Award Type',
    empty_select => '-- Select One --',
    do_not_reload => 1,
    options_method => sub { $_[0]->form->_options_from_enum($_[0]->name) },
);

# TODO: this should be pulled back into HTML::FormHandler::Model::DBIC or
# HTML::FormHandler::Field::Select
sub _options_from_enum {
    my ($self, $field_name) = @_;

    my $values = $self->schema->source($self->source_name)->column_info($field_name)->{extra}{list};
    my %options;
    @options{@$values} = @$values;
    [ %options ]
}