[html-formfu] HTML::FormFu::Element::DBIC::Select for review
Mario Minati
mario.minati at googlemail.com
Mon Sep 24 19:20:52 GMT 2007
On Monday 24 September 2007 18:39:05 Zbigniew Lukasiak wrote:
> Here is my take on the subject - HTML::FormFu::Element::Select::DBIC.
> It overrides setup - seems like this is simpler than both render and
> process - but setup is not documented so I don't know if that is the
> right way.
>
> Below is the code - very short. I attach also a package - together
> with some tests.
>
> I am also going to extend the fill_formfu_values from
> DBIx::Class::HTML::FormFu so that it works with has_many and
> many_to_many relations but for that I need first a 'multi' Select
> field.
>
> Cheers,
> Zbyszek
>
> -------------------------------------------------------------
>
> package HTML::FormFu::Element::Select::DBIC;
> use strict;
>
> BEGIN {
> use vars qw($VERSION );
> $VERSION = '0.01';
> }
>
> use base 'HTML::FormFu::Element::Select';
> use Carp qw/ croak /;
>
> __PACKAGE__->mk_accessors( qw/
> schema
> resultset_name
> where
> search_attr
> presentation
> value_field
> / );
>
> sub setup {
> #sub new {
> my $self = shift;
> $self->next::method(@_);
> my $stash = $self->form->{stash};
> my $schema = $stash->{'schema'};
> my $resultset_name = $self->resultset_name || ucfirst $self->name;
> croak "Needs a DBIx::Class schema and a ResultSet class name"
> if ( ! $schema) & ( ! $resultset_name );
> my $rs = $schema->resultset( $resultset_name )->search( $self->where,
> $self->search_attr );
> my $value_field = $self->value_field || 'id';
> my $presentation = $self->presentation || 'id';
> my @options = map { [ $_->$value_field, $_->$presentation ] } $rs->all;
> $self->options( \@options );
> }
>
> 1;
I don't think setup is the right place to do this work.
When you look at what is happening when creating an element with perl not YAML
like in 04basic.t:
The element is created with a call to 'element' (defined in ObjectUtil, around
line 623).
This functions calls '_single_element' (defined in ObjectUtil, around line
721).
This function calls '_require_element' (defined in ObjectUtil, around line
74).
And here the 'setup' method of the element is invoked.
It works fine as long you create your element with all data in place like with
YAML, but it will fail when doing the chained creation like in the mentioned
testfile 04basic.t.
Sorry, didn't want to disappoint you.
The assumption to use 'id' also for presentation is nice.
Greets,
Mario
More information about the HTML-FormFu
mailing list