| 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__ |
| 61 |
|
| 62 |
=pod |
| 63 |
|
| 64 |
=encoding UTF-8 |
| 65 |
|
| 66 |
=head1 NAME |
| 67 |
|
| 68 |
HTML::FormHandler::Foo - Experiment in loading form from config file |
| 69 |
|
| 70 |
=head1 VERSION |
| 71 |
|
| 72 |
version 0.410002 |
| 73 |
|
| 74 |
=head1 AUTHOR |
| 75 |
|
| 76 |
FormHandler Contributors - see HTML::FormHandler |
| 77 |
|
| 78 |
=head1 COPYRIGHT AND LICENSE |
| 79 |
|
| 80 |
This software is copyright (c) 2017 by Gerda Shank. |
| 81 |
|
| 82 |
This is free software; you can redistribute it and/or modify it under |
| 83 |
the same terms as the Perl 5 programming language system itself. |
| 84 |
|
| 85 |
=cut |