1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
package HTML::FormHandler::Fields;
# ABSTRACT: internal role for form and compound fields
use Moose::Role;
use HTML::FormHandler::TraitFor::Types;
has 'fields' => (
traits => ['Array'],
isa => 'ArrayRef[HTML::FormHandler::Field]',
is => 'rw',
default => sub { [] },
auto_deref => 1,
handles => {
all_fields => 'elements',
clear_fields => 'clear',
add_field => 'push',
push_field => 'push',
num_fields => 'count',
has_fields => 'count',
set_field_at => 'set',
_pop_field => 'pop',
}
);
# This is for updates applied via roles or compound field classes; allows doing
# both updates on the process call and updates from class applied roles
has 'update_subfields' => ( is => 'rw', isa => 'HashRef', builder => 'build_update_subfields',
traits => ['Hash'], handles => { clear_update_subfields => 'clear',
has_update_subfields => 'count' } );
sub build_update_subfields {{}}
# used to transfer tags to fields from form and compound fields
has 'widget_tags' => (
isa => 'HashRef',
traits => ['Hash'],
is => 'rw',
default => sub {{}},
handles => {
has_widget_tags => 'count'
}
);
# compatibility wrappers for result errors
sub error_fields {
my $self = shift;
return map { $_->field_def } @{ $self->result->error_results };
}
sub has_error_fields { shift->result->has_error_results }
sub add_error_field {
my ( $self, $field ) = @_;
$self->result->add_error_result( $field->result );
}
sub num_error_fields { shift->result->num_error_results }
has 'field_name_space' => (
isa => 'HFH::ArrayRefStr',
is => 'rw',
traits => ['Array'],
lazy => 1,
default => '',
coerce => 1,
handles => {
add_field_name_space => 'push',
},
);
sub field_index {
my ( $self, $name ) = @_;
my $index = 0;
for my $field ( $self->all_fields ) {
return $index if $field->name eq $name;
$index++;
}
return;
}
sub subfield {
my ( $self, $name ) = @_;
return $self->field($name, undef, $self);
}
sub field {
my ( $self, $name, $die, $f ) = @_;
my $index;
# if this is a full_name for a compound field
# walk through the fields to get to it
return undef unless ( defined $name );
if( $self->form && $self == $self->form &&
exists $self->index->{$name} ) {
return $self->index->{$name};
}
if ( $name =~ /\./ ) {
my @names = split /\./, $name;
$f ||= $self->form || $self;
foreach my $fname (@names) {
$f = $f->field($fname);
return unless $f;
}
return $f;
}
else # not a compound name
{
for my $field ( $self->all_fields ) {
return $field if ( $field->name eq $name );
}
}
return unless $die;
die "Field '$name' not found in '$self'";
}
sub sorted_fields {
my $self = shift;
my @fields = sort { $a->order <=> $b->order }
grep { $_->is_active } $self->all_fields;
return wantarray ? @fields : \@fields;
}
# the routine for looping through and processing each field
sub _fields_validate {
my $self = shift;
return unless $self->has_fields;
# validate all fields
my %value_hash;
foreach my $field ( $self->all_fields ) {
next if ( $field->is_inactive || $field->disabled || !$field->has_result );
# Validate each field and "inflate" input -> value.
$field->validate_field; # this calls the field's 'validate' routine
$value_hash{ $field->accessor } = $field->value
if ( $field->has_value && !$field->noupdate );
}
$self->_set_value( \%value_hash );
}
sub fields_set_value {
my $self = shift;
my %value_hash;
foreach my $field ( $self->all_fields ) {
next if ( $field->is_inactive || !$field->has_result );
$value_hash{ $field->accessor } = $field->value
if ( $field->has_value && !$field->noupdate );
}
$self->_set_value( \%value_hash );
}
sub fields_fif {
my ( $self, $result, $prefix ) = @_;
$result ||= $self->result;
return unless $result;
$prefix ||= '';
if ( $self->isa('HTML::FormHandler') ) {
$prefix = $self->name . "." if $self->html_prefix;
}
my %params;
foreach my $fld_result ( $result->results ) {
my $field = $fld_result->field_def;
next if ( $field->is_inactive || $field->password );
my $fif = $fld_result->fif;
next if ( !defined $fif || (ref $fif eq 'ARRAY' && ! scalar @{$fif} ) );
if ( $fld_result->has_results ) {
my $next_params = $fld_result->fields_fif( $prefix . $field->name . '.' );
next unless $next_params;
%params = ( %params, %{$next_params} );
}
else {
$params{ $prefix . $field->name } = $fif;
}
}
return if !%params;
return \%params;
}
sub clear_data {
my $self = shift;
$self->clear_result;
$self->clear_active;
$_->clear_data for $self->all_fields;
}
sub propagate_error {
my ( $self, $result ) = @_;
# References to fields with errors are propagated up the tree.
# All fields with errors should end up being in the form's
# error_results. Once.
my ($found) = grep { $_ == $result } $self->result->all_error_results;
unless ( $found ) {
$self->result->add_error_result($result);
if ( $self->parent ) {
$self->parent->propagate_error( $result );
}
}
}
sub dump_fields { shift->dump(@_) }
sub dump {
my $self = shift;
warn "HFH: ------- fields for ", $self->name, "-------\n";
for my $field ( $self->sorted_fields ) {
$field->dump;
}
warn "HFH: ------- end fields -------\n";
}
sub dump_validated {
my $self = shift;
warn "HFH: fields validated:\n";
foreach my $field ( $self->sorted_fields ) {
$field->dump_validated if $field->can('dump_validated');
my $message = $field->has_errors ? join( ' | ', $field->all_errors) : 'validated';
warn "HFH: ", $field->name, ": $message\n";
}
}
use namespace::autoclean;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
HTML::FormHandler::Fields - internal role for form and compound fields
=head1 VERSION
version 0.40057
=head1 SYNOPSIS
A role to implement field attributes, accessors, etc. To be applied
to L<HTML::FormHandler> and L<HTML::FormHandler::Field::Compound>.
=head2 fields
The field definitions as built from the field_list and the 'has_field'
declarations. This provides clear_fields, add_field, remove_last_field,
num_fields, has_fields, and set_field_at methods.
=head2 field( $full_name )
Return the field object with the full_name passed. Will return undef
if the field is not found, or will die if passed a second parameter.
=head2 field_index
Convenience function for use with 'set_field_at'. Pass in 'name' of field
(not full_name)
=head2 sorted_fields
Calls fields and returns them in sorted order by their "order"
value. Non-sorted fields are retrieved with 'fields'.
=head2 clear methods
clear_data
clear_fields
clear_error_fields
=head2 Dump information
dump - turn verbose flag on to get this output
dump_validated - shorter version
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Gerda Shank.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|