| 1 |
package HTML::FormHandler::Field::Display; |
| 2 |
# ABSTRACT: display only field |
| 3 |
$HTML::FormHandler::Field::Display::VERSION = '0.410002'; |
| 4 |
use Moose; |
| 5 |
extends 'HTML::FormHandler::Field::NoValue'; |
| 6 |
use namespace::autoclean; |
| 7 |
|
| 8 |
|
| 9 |
has 'html' => ( is => 'rw', isa => 'Str', builder => 'build_html', lazy => 1 ); |
| 10 |
sub build_html {''} |
| 11 |
has 'set_html' => ( isa => 'Str', is => 'ro'); |
| 12 |
has '+do_label' => ( default => 0 ); |
| 13 |
|
| 14 |
has 'render_method' => ( |
| 15 |
traits => ['Code'], |
| 16 |
is => 'ro', |
| 17 |
isa => 'CodeRef', |
| 18 |
lazy => 1, |
| 19 |
predicate => 'does_render_method', |
| 20 |
handles => { 'render' => 'execute_method' }, |
| 21 |
builder => 'build_render_method', |
| 22 |
); |
| 23 |
|
| 24 |
sub build_render_method { |
| 25 |
my $self = shift; |
| 26 |
|
| 27 |
my $set_html = $self->set_html; |
| 28 |
$set_html ||= "html_" . HTML::FormHandler::Field::convert_full_name($self->full_name); |
| 29 |
return sub { my $self = shift; $self->form->$set_html($self); } |
| 30 |
if ( $self->form && $self->form->can($set_html) ); |
| 31 |
return sub { |
| 32 |
my $self = shift; |
| 33 |
return $self->html; |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
sub _result_from_object { |
| 38 |
my ( $self, $result, $value ) = @_; |
| 39 |
$self->_set_result($result); |
| 40 |
$self->value($value); |
| 41 |
$result->_set_field_def($self); |
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
__PACKAGE__->meta->make_immutable; |
| 46 |
use namespace::autoclean; |
| 47 |
1; |
| 48 |
|
| 49 |
__END__ |
| 50 |
|
| 51 |
=pod |
| 52 |
|
| 53 |
=encoding UTF-8 |
| 54 |
|
| 55 |
=head1 NAME |
| 56 |
|
| 57 |
HTML::FormHandler::Field::Display - display only field |
| 58 |
|
| 59 |
=head1 VERSION |
| 60 |
|
| 61 |
version 0.410002 |
| 62 |
|
| 63 |
=head1 SYNOPSIS |
| 64 |
|
| 65 |
This class can be used for fields that are display only. It will |
| 66 |
render the value returned by a form's 'html_<field_name>' method, |
| 67 |
or the field's 'html' attribute. |
| 68 |
|
| 69 |
has_field 'explanation' => ( type => 'Display', |
| 70 |
html => '<p>This is an explanation...</p>' ); |
| 71 |
|
| 72 |
or in a form: |
| 73 |
|
| 74 |
has_field 'explanation' => ( type => 'Display' ); |
| 75 |
sub html_explanation { |
| 76 |
my ( $self, $field ) = @_; |
| 77 |
if( $self->something ) { |
| 78 |
return '<p>This type of explanation...</p>'; |
| 79 |
} |
| 80 |
else { |
| 81 |
return '<p>Another type of explanation...</p>'; |
| 82 |
} |
| 83 |
} |
| 84 |
#---- |
| 85 |
has_field 'username' => ( type => 'Display' ); |
| 86 |
sub html_username { |
| 87 |
my ( $self, $field ) = @_; |
| 88 |
return '<div><b>User: </b>' . $field->value . '</div>'; |
| 89 |
} |
| 90 |
|
| 91 |
or set the name of the rendering method: |
| 92 |
|
| 93 |
has_field 'explanation' => ( type => 'Display', set_html => 'my_explanation' ); |
| 94 |
sub my_explanation { |
| 95 |
.... |
| 96 |
} |
| 97 |
|
| 98 |
or provide a 'render_method': |
| 99 |
|
| 100 |
has_field 'my_button' => ( type => 'Display', render_method => \&render_my_button ); |
| 101 |
sub render_my_button { |
| 102 |
my $self = shift; |
| 103 |
.... |
| 104 |
return '...'; |
| 105 |
} |
| 106 |
|
| 107 |
=head1 AUTHOR |
| 108 |
|
| 109 |
FormHandler Contributors - see HTML::FormHandler |
| 110 |
|
| 111 |
=head1 COPYRIGHT AND LICENSE |
| 112 |
|
| 113 |
This software is copyright (c) 2017 by Gerda Shank. |
| 114 |
|
| 115 |
This is free software; you can redistribute it and/or modify it under |
| 116 |
the same terms as the Perl 5 programming language system itself. |
| 117 |
|
| 118 |
=cut |