| 1 | package HTML::FormHandler::Foo; | ||
| 2 | # ABSTRACT: Experiment in loading form from config file | ||
| 3 | $HTML::FormHandler::Foo::VERSION = '0.410002'; | ||
| 4 | use Moose; | ||
| 5 | extends 'HTML::FormHandler'; | ||
| 6 | with 'HTML::FormHandler::Render::WithTT'; | ||
| 7 | |||
| 8 | use Config::Any; | ||
| 9 | |||
| 10 | has 'form_error_message' => ( isa => 'Str', is => 'rw' ); | ||
| 11 | has 'javascript_src' => ( isa => 'Str', is => 'rw' ); | ||
| 12 | has 'javascript' => ( isa => 'Str', is => 'rw' ); | ||
| 13 | |||
| 14 | sub before_build { | ||
| 15 | my $self = shift; | ||
| 16 | |||
| 17 | $self->add_tt_include_path('share/templates/foo'); | ||
| 18 | $self->process_config_file; | ||
| 19 | $self->process_config; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub build_tt_template { 'form.tt' } | ||
| 23 | |||
| 24 | has 'config_file' => ( isa => 'Str', is => 'rw' ); | ||
| 25 | has 'config' => ( isa => 'HashRef', is => 'rw' ); | ||
| 26 | |||
| 27 | sub submitted_and_valid { shift->validated } | ||
| 28 | |||
| 29 | sub process_config_file { | ||
| 30 | my $self = shift; | ||
| 31 | return unless $self->config_file; | ||
| 32 | unless ( -e $self->config_file ) { | ||
| 33 | die "form config file " . $self->config_file . " . does not exist"; | ||
| 34 | } | ||
| 35 | my $config = Config::Any->load_files({ | ||
| 36 | files => [$self->config_file], | ||
| 37 | use_ext => 1, | ||
| 38 | driver_args => { General => { -UTF8 => 1 }, }, | ||
| 39 | }); | ||
| 40 | $config = $config->[0]->{$self->config_file}; | ||
| 41 | $self->config($config); | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | sub process_config { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | my $config = $self->config; | ||
| 49 | while ( my ( $key, $value ) = each %{$config} ) { | ||
| 50 | confess "invalid attribute '$key' in form config" | ||
| 51 | unless $self->can($key); | ||
| 52 | $self->$key($value); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | __PACKAGE__->meta->make_immutable; | ||
| 57 | use namespace::autoclean; | ||
| 58 | 1; | ||
| 59 | |||
| 60 | __END__ |