Advertisement
cng

HFH field order and *_options method

cng
Jan 2nd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.15 KB | None | 0 0
  1. package Forms::Address;
  2.  
  3. use HTML::FormHandler::Moose;
  4. extends  'HTML::FormHandler';
  5. with     'HTML::FormHandler::TraitFor::Model::DBIC';
  6.  
  7. has_field 'addr_country' => (
  8.     type         => 'Select',
  9.     label        => 'Country',
  10.     order        => 1,
  11.     empty_select => '-- Choose a Country --',
  12.     options      => [
  13.         { value => 'AU', label => 'AU' },
  14.         { value => 'NZ', label => 'NZ' },
  15.     ],
  16. );
  17.  
  18. has_field 'addr_state' => (
  19.     type           => 'Select',
  20.     label          => 'State',
  21.     order          => 2,
  22.     empty_select   => '-- Select State --',
  23.     options_method => \&options_addr_state,
  24. );
  25.  
  26. sub options_addr_state {
  27.     my $self = shift;
  28.     return unless $self->form->schema;
  29.     my $country      = $self->form->field('addr_country');
  30.     my $country_code = $country->value;
  31.     my @values       = $self->form->schema->resultset('States')->search(
  32.         { country_code => $country_code, state => { '!=' => '' } },
  33.         {
  34.             select   => [{ distinct => ['state'] }],
  35.             as       => ['state'],
  36.             order_by => 'state',
  37.         }
  38.     )->get_column('state')->all;
  39.     return [map { value=>$_, label=>$_ } @values];
  40. } ## end sub options_addr_state
  41.  
  42. __PACKAGE__->meta->make_immutable;
  43. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement