File Coverage

blib/lib/Rose/HTML/Form/Field.pm
Criterion Covered Total %
statement 412 461 89.3
branch 178 238 74.7
condition 62 101 61.3
subroutine 88 92 95.6
pod 47 66 71.2
total 787 958 82.1


line stmt bran cond sub pod time code
1             package Rose::HTML::Form::Field;
2              
3 35     35   108571 use strict;
  35         106  
  35         1118  
4              
5 35     35   195 use Carp();
  35         79  
  35         494  
6 35     35   189 use Scalar::Util();
  35         66  
  35         529  
7              
8 35     35   10993 use Rose::HTML::Util();
  35         100  
  35         838  
9              
10 35     35   15177 use Rose::HTML::Label;
  35         129  
  35         283  
11 35     35   234 use Rose::HTML::Object::Errors qw(:field);
  35         74  
  35         323  
12 35     35   232 use Rose::HTML::Object::Messages qw(:field);
  35         126  
  35         215  
13              
14 35     35   254 use base 'Rose::HTML::Object';
  35         133  
  35         4343  
15              
16 35     35   264 use constant HTML_ERROR_SEP => "<br>\n";
  35         80  
  35         2964  
17 35     35   253 use constant XHTML_ERROR_SEP => "<br />\n";
  35         83  
  35         2127  
18              
19 35     35   13762 use Rose::HTML::Form::Constants qw(FF_SEPARATOR);
  35         102  
  35         3063  
20              
21             our $VERSION = '0.617';
22              
23             #our $Debug = 0;
24              
25             use Rose::HTML::Object::MakeMethods::Localization
26             (
27 35         374 localized_message =>
28             [
29             qw(_error_label label description)
30             ],
31 35     35   244 );
  35         99  
32              
33             use Rose::Object::MakeMethods::Generic
34             (
35 35         541 scalar => [ qw(rank type) ],
36              
37             boolean => [ qw(required is_cleared has_partial_value) ],
38             boolean =>
39             [
40             trim_spaces => { default => 1 },
41             empty_is_ok => { default => 0 },
42             ],
43              
44             'scalar --get_set_init' =>
45             [
46             qw(html_prefix html_suffix html_error_separator xhtml_error_separator
47             local_moniker apply_error_class)
48             ],
49 35     35   3478 );
  35         100  
50              
51             __PACKAGE__->add_valid_html_attrs(qw(
52             name
53             value
54             onblur
55             onfocus
56             accesskey
57             tabindex
58             autofocus
59             ));
60              
61 20     20 0 48 sub is_button { 0 }
62              
63             *label_id = \&label_message_id;
64              
65             sub error_label
66             {
67 61     61 1 117 my($self) = shift;
68              
69 61 100       161 if(@_)
70             {
71 2         11 return $self->_error_label(@_);
72             }
73              
74 59         194 my $label = $self->_error_label;
75              
76 35     35   5747 no warnings 'uninitialized';
  35         82  
  35         4724  
77 59 100       247 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    100          
78             }
79              
80             sub error_label_id
81             {
82 128     128 1 838 my($self) = shift;
83              
84 128 50       310 if(@_)
85             {
86 128         449 return $self->_error_label_message_id(@_);
87             }
88              
89 0         0 my $label = $self->_error_label_message_id;
90              
91 35     35   267 no warnings 'uninitialized';
  35         98  
  35         36652  
92 0 0       0 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    0          
93             }
94              
95 302     302 0 3921 sub init_apply_error_class { 1 }
96              
97             sub auto_invalidate_parent
98             {
99 3182     3182 1 5122 my($self) = shift;
100              
101 3182 50       6003 if(@_)
102             {
103 0 0       0 return $self->{'auto_invalidate_parent'} = $_[0] ? 1 : 0;
104             }
105              
106             return defined($self->{'auto_invalidate_parent'}) ?
107             $self->{'auto_invalidate_parent'} :
108 3182 100       9127 ($self->{'auto_invalidate_parent'} = 1);
109             }
110              
111             sub invalidate_value
112             {
113 89     89 1 160 $_[0]->{'input_value'} = undef;
114 89         152 $_[0]->{'internal_value'} = undef;
115 89         194 $_[0]->{'output_value'} = undef;
116             }
117              
118             sub invalidate_output_value
119             {
120 1     1 1 4 $_[0]->{'output_value'} = undef;
121             }
122              
123             sub parent_group
124             {
125 5543     5543 1 8962 my($self) = shift;
126              
127 5543 100       10397 if(@_)
128             {
129 249 50       579 if(ref $_[0])
130             {
131 249         1087 Scalar::Util::weaken($self->{'parent_group'} = shift);
132 249         670 return $self->{'parent_group'};
133             }
134             else
135             {
136 0         0 return $self->{'parent_group'} = shift;
137             }
138             }
139              
140 5294         15500 return $self->{'parent_group'};
141             }
142              
143             sub parent_field
144             {
145 36159     36159 1 52403 my($self) = shift;
146              
147 36159 100       65664 if(@_)
148             {
149 317 50       721 if(ref $_[0])
150             {
151 317         1309 Scalar::Util::weaken($self->{'parent_field'} = shift);
152 317         841 return $self->{'parent_field'};
153             }
154             else
155             {
156 0         0 return $self->{'parent_field'} = shift;
157             }
158             }
159              
160 35842         94415 return $self->{'parent_field'};
161             }
162              
163             sub parent_form
164             {
165 43654     43654 1 61512 my($self) = shift;
166              
167 43654 100       76432 if(@_)
168             {
169 329 50       805 if(ref $_[0])
170             {
171 329         1437 Scalar::Util::weaken($self->{'parent_form'} = shift);
172 329         742 return $self->{'parent_form'};
173             }
174             else
175             {
176 0         0 return $self->{'parent_form'} = shift;
177             }
178             }
179              
180 43325         107965 return $self->{'parent_form'};
181             }
182              
183             sub field_depth
184             {
185 0     0 0 0 my($self) = shift;
186              
187 0   0     0 my $parent = $self->parent_field || $self->parent_form;
188              
189 0 0       0 return 0 unless($parent);
190              
191 0         0 my $depth = 1;
192 0   0     0 $depth++ while($parent = $parent->parent_field || $parent->parent_form);
193              
194 0         0 return $depth;
195             }
196              
197             sub fq_name
198             {
199 14485     14485 0 21117 my($self) = shift;
200 14485         24674 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  43455         97960  
201             $self->field_context_name,
202             $self->local_name);
203             }
204              
205             sub fq_moniker
206             {
207 1802     1802 0 2780 my($self) = shift;
208              
209 1802         3155 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  5406         21141  
210             $self->field_context_name,
211             $self->local_moniker);
212             }
213              
214 62     62 0 546 sub init_local_moniker { shift->local_name }
215              
216             sub form_context_name
217             {
218 17858     17858 0 25114 my($self) = shift;
219 17858 100       28821 my $parent_form = $self->parent_form or return undef;
220 7397         16908 return $parent_form->fq_form_name;
221             }
222              
223             sub field_context_name
224             {
225 17858     17858 0 27012 my($self) = shift;
226 17858 100       29806 my $parent_field = $self->parent_field or return undef;
227 7539 50       13499 my $name = $parent_field->fq_name or return undef;
228 7539         17652 return $name;
229             }
230              
231 11     11 0 25 sub is_flat_group { 0 }
232              
233 32     32 0 393 sub init_html_prefix { '' }
234 32     32 0 591 sub init_html_suffix { '' }
235              
236 5     5 0 82 sub init_html_error_separator { HTML_ERROR_SEP }
237 4     4 0 88 sub init_xhtml_error_separator { XHTML_ERROR_SEP }
238              
239             sub value
240             {
241 167     167 1 892 my($self) = shift;
242              
243 167 50       485 if(@_)
244             {
245 167         447 return $self->input_value($self->html_attr('value', shift));
246             }
247             else
248             {
249 0         0 return $self->html_attr('value');
250             }
251             }
252              
253             sub resync_name
254             {
255 3771     3771 0 6228 my($self) = shift;
256              
257 3771         9795 $self->html_attr('name', undef);
258 3771 50 66     8087 $self->name if($self->parent_field || $self->parent_form);
259             #$self->name($self->fq_name);
260             }
261              
262             sub local_name
263             {
264 17455     17455 1 26131 my($self) = shift;
265              
266 17455 100       30904 if(@_)
267             {
268 1717         2701 my $name = shift;
269              
270 35     35   275 no warnings 'uninitialized';
  35         91  
  35         9733  
271 1717 50 66     5192 if(index($name, FF_SEPARATOR) >= 0 && !$self->isa('Rose::HTML::Form::Field::Hidden'))
272             {
273 0         0 Carp::croak "Invalid local field name: $name";
274             }
275              
276 1717         3258 my $old_name = $self->{'local_name'};
277 1717         2943 $self->{'local_name'} = $name;
278              
279 1717 100 100     5250 if(defined $old_name && $name ne $old_name)
280             {
281 3 100       10 if(my $parent_form = $self->parent_form)
282             {
283 1         12 $parent_form->delete_field($old_name);
284 1         10 $parent_form->add_field($name => $self);
285             }
286              
287 3 50       9 if(my $parent_field = $self->parent_field)
288             {
289 0         0 $parent_field->delete_field($old_name);
290 0         0 $parent_field->add_field($name => $self);
291             }
292             }
293              
294 1717         3408 return $name;
295             }
296              
297 15738         24986 my $name = $self->{'local_name'};
298 15738 100       36257 return $name if(defined $name);
299 468         1292 return $self->{'local_name'} = $self->{'local_moniker'};
300             }
301              
302             sub name
303             {
304 8078     8078 1 16253 my($self) = shift;
305              
306 8078 100       15569 if(@_)
307             {
308 562         1572 $self->local_name(shift);
309 562         1546 return $self->html_attr('name', $self->fq_name);
310             }
311              
312 7516         15649 my $name = $self->html_attr('name');
313              
314             # The name HTML attr will be an empty string if it's a required attr,
315             # so use length() and not defined()
316 35     35   271 no warnings 'uninitialized';
  35         85  
  35         37859  
317 7516 100       15421 unless(length $name)
318             {
319 2986         6429 return $self->html_attr('name', $self->fq_name);
320             }
321              
322 4530         12696 return $name;
323             }
324              
325             sub moniker
326             {
327 758     758 0 1311 my($self) = shift;
328              
329 758 100       1524 if(@_)
330             {
331 257         905 return $self->fq_moniker($self->{'moniker'} = shift);
332             }
333             else
334             {
335 501 100       1595 return $self->{'moniker'} if(defined $self->{'moniker'});
336 57         194 return $self->{'moniker'} = $self->fq_moniker;
337             }
338             }
339              
340             sub default_value
341             {
342 4815     4815 1 7359 my($self) = shift;
343              
344 4815 100       8620 if(@_)
345             {
346 546         1120 $self->{'internal_value'} = undef;
347 546         949 $self->{'output_value'} = undef;
348 546         1605 return $self->{'default_value'} = shift;
349             }
350              
351 4269         9096 return $self->{'default_value'};
352             }
353              
354 30     30 1 328 sub default { shift->default_value(@_) }
355              
356 1549     1549 1 2616 sub inflate_value { $_[1] }
357 254     254 1 543 sub deflate_value { $_[1] }
358              
359             sub input_value
360             {
361 5944     5944 1 26948 my($self) = shift;
362              
363 5944 100       11138 if(@_)
364             {
365 2921         4554 $self->{'is_cleared'} = 0;
366 2921         4530 $self->{'internal_value'} = undef;
367 2921         4621 $self->{'output_value'} = undef;
368 2921         4667 $self->{'errors'} = undef;
369 2921         4708 $self->{'input_value'} = shift;
370              
371 2921 100       5546 if(my $parent = $self->parent_field)
372             {
373 1211 100 100     3152 $parent->is_cleared(0) if(!$parent->{'in_init'} && $parent->_is_full);
374              
375 1211 100       3124 if($self->auto_invalidate_parent)
376             {
377 54         215 $parent->invalidate_value;
378             }
379             }
380              
381 2921         7667 return $self->{'input_value'};
382             }
383              
384 3023 100 100     6461 return undef if($self->is_cleared || $self->has_partial_value);
385              
386             my $value =
387 2954 100       31358 (defined $self->{'input_value'}) ? $self->{'input_value'} :
388             $self->default_value;
389              
390 2954 50 33     6399 if(wantarray && ref $value eq 'ARRAY')
391             {
392 0         0 return @$value;
393             }
394              
395 2954         5705 return $value;
396             }
397              
398             sub _set_input_value
399             {
400             # XXX: Evil, but I can't bear to add 3 method calls to
401             # XXX: save and then restore this value.
402 2462     2462   5045 local $_[0]->{'auto_invalidate_parent'} = 0;
403 2462         5425 shift->input_value(@_);
404             }
405              
406             sub _is_full
407             {
408 1314     1314   2045 my($self) = shift;
409              
410 1314 100       3441 if($self->is_full(@_))
411             {
412 206         726 $self->has_partial_value(0);
413 206         1502 return 1;
414             }
415              
416 1108         4261 return 0;
417             }
418              
419             sub input_value_filtered
420             {
421 78     78 1 165 my($self) = shift;
422              
423 78         215 my $value = $self->input_value;
424              
425 78         229 $value = $self->input_prefilter($value);
426              
427 78 100       214 if(my $input_filter = $self->input_filter)
428             {
429 4         15 local $_ = $value;
430 4         16 $value = $input_filter->($self, $value);
431             }
432              
433 78         341 return $value;
434             }
435              
436             sub internal_value
437             {
438 7302     7302 1 13324 my($self) = shift;
439              
440 7302 50       13547 Carp::croak "Cannot set the internal value. Use input_value() instead." if(@_);
441              
442 7302 100 100     14098 return undef if($self->is_cleared || $self->has_partial_value);
443              
444 3320 100       34980 if(defined $self->{'internal_value'})
445             {
446 1274 100 100     3420 if(wantarray && ref $self->{'internal_value'} eq 'ARRAY')
447             {
448 2         42 return @{$self->{'internal_value'}};
  2         14  
449             }
450              
451 1272         3905 return $self->{'internal_value'};
452             }
453              
454 2046         4083 my $value = $self->input_value;
455              
456 2046         3392 my($using_default, $final_value);
457              
458 2046 100       3928 unless(defined $value)
459             {
460 1361         2452 $value = $self->default_value;
461 1361         2216 $using_default++;
462             }
463              
464 2046         4289 $value = $self->input_prefilter($value);
465              
466 2046 100       4151 if(my $input_filter = $self->input_filter)
467             {
468 29         84 local $_ = $value;
469 29         78 $final_value = $input_filter->($self, $value);
470             }
471 2017         3036 else { $final_value = $value }
472              
473 2046         4917 $final_value = $self->inflate_value($final_value);
474              
475 2046 100       4315 $self->{'internal_value'} = $final_value unless($using_default);
476              
477 2046 100 100     5528 if(wantarray && ref $final_value eq 'ARRAY')
478             {
479 2         10 return @$final_value;
480             }
481              
482 2044         6145 return $final_value;
483             }
484              
485 0     0 0 0 sub internal_value_singular { shift->internal_value(@_) }
486              
487             sub output_value
488             {
489 554     554 1 10113 my($self) = shift;
490              
491 554 50       1302 Carp::croak "Cannot set the output value. Use input_value() instead." if(@_);
492              
493 554 100       1441 return undef if($self->is_cleared);
494              
495 498 100       3751 return $self->{'output_value'} if(defined $self->{'output_value'});
496              
497 322         881 my $value = $self->deflate_value(scalar $self->internal_value);
498              
499 322 100       5818 if(my $output_filter = $self->output_filter)
500             {
501 22         54 local $_ = $value;
502 22         81 $self->{'output_value'} = $output_filter->($self, $value);
503             }
504 300         601 else { $self->{'output_value'} = $value }
505              
506 322 50 66     1398 if(wantarray && ref $self->{'output_value'} eq 'ARRAY')
507             {
508 0         0 return @{$self->{'output_value'}};
  0         0  
509             }
510              
511 322         1390 return $self->{'output_value'};
512             }
513              
514             sub is_empty
515             {
516 3605     3605 1 5439 my($self) = shift;
517              
518 3605         5991 my $value = $self->internal_value;
519              
520 35     35   295 no warnings;
  35         133  
  35         5279  
521 3605 100 66     21502 return ($value =~ /\S/ || (!$self->trim_spaces && length $value)) ? 0 : 1;
522             }
523              
524 0     0 1 0 sub is_full { !shift->is_empty }
525              
526             sub input_prefilter
527             {
528 2124     2124 1 3707 my($self, $value) = @_;
529              
530 2124 100       4415 return undef unless(defined $value);
531              
532 739 100       1579 unless(ref $value)
533             {
534 702         1353 for($value)
535             {
536 35     35   270 no warnings;
  35         82  
  35         15889  
537              
538 702 100       1731 if($self->trim_spaces)
539             {
540 700         5613 s/^\s+//;
541 700         2145 s/\s+$//;
542             }
543             }
544             }
545              
546 739         1684 return $value;
547             }
548              
549             sub input_filter
550             {
551 2135     2135 1 3962 my($self) = shift;
552              
553 2135 100       4093 if(@_)
554             {
555 11         29 $self->{'internal_value'} = undef;
556 11         30 $self->{'output_value'} = undef;
557 11         39 return $self->{'input_filter'} = shift;
558             }
559              
560 2124         4614 return $self->{'input_filter'};
561             }
562              
563             sub output_filter
564             {
565 337     337 1 3199 my($self) = shift;
566              
567 337 100       715 if(@_)
568             {
569 14         32 $self->{'output_value'} = undef;
570 14         57 return $self->{'output_filter'} = shift;
571             }
572              
573 323         826 return $self->{'output_filter'};
574             }
575              
576             sub filter
577             {
578 1     1 1 3 my($self) = shift;
579              
580 1 50       4 if(@_)
581             {
582 1         2 my $filter = shift;
583 1         5 $self->{'input_filter'} = $filter;
584 1         4 $self->{'output_filter'} = $filter;
585             }
586              
587 1         3 my $input_filter = $self->{'input_filter'};
588              
589 1 50 33     6 if(ref $input_filter && $input_filter eq $self->output_filter)
590             {
591 1         4 return $input_filter;
592             }
593              
594 0         0 return;
595             }
596              
597             sub clear
598             {
599 1808     1808 1 3370 my($self) = shift;
600              
601 1808         4051 $self->_set_input_value(undef);
602 1808         5188 $self->error(undef);
603 1808         4583 $self->has_partial_value(0);
604 1808         7363 $self->is_cleared(1);
605             }
606              
607             sub reset
608             {
609 66     66 1 163 my($self) = shift;
610              
611 66         189 $self->_set_input_value(undef);
612 66         310 $self->error(undef);
613 66         222 $self->has_partial_value(0);
614 66         348 $self->is_cleared(0);
615 66         312 return 1;
616             }
617              
618             sub hidden_fields
619             {
620 115     115 1 907 my($self) = shift;
621              
622 115         409 my $hidden_field_class = ref($self)->object_type_class_loaded('hidden');
623              
624 35     35   311 no strict 'refs';
  35         97  
  35         34167  
625 115 50       203 unless(@{$hidden_field_class . '::ISA'})
  115         627  
626             {
627 0         0 my $error;
628              
629             TRY:
630             {
631 0         0 local $@;
  0         0  
632 0         0 eval "use $hidden_field_class";
633 0         0 $error = $@;
634             }
635              
636 0 0       0 Carp::croak "Could not load hidden field class '$hidden_field_class' - $error"
637             if($error);
638             }
639              
640             return
641 115         337 $hidden_field_class->new(
642             name => $self->html_attr('name'),
643             id => $self->html_attr('id'),
644             class => $self->html_attr('class'),
645             value => $self->output_value);
646             }
647              
648 86     86 1 307 sub hidden_field { shift->hidden_fields(@_) }
649              
650             sub html_hidden_fields
651             {
652 12     12 1 35 my($self) = shift;
653              
654 12         24 my @html;
655              
656 12         58 foreach my $field ($self->hidden_fields)
657             {
658 18         69 push(@html, $field->html_field);
659             }
660              
661 12 50       184 return (wantarray) ? @html : join("\n", @html);
662             }
663              
664 8     8 1 60 sub html_hidden_field { shift->html_hidden_fields(@_) }
665              
666             sub xhtml_hidden_fields
667             {
668 7     7 1 22 my($self) = shift;
669              
670 7         16 my @xhtml;
671              
672 7         29 foreach my $field ($self->hidden_fields)
673             {
674 9         53 push(@xhtml, $field->xhtml_field);
675             }
676              
677 7 50       95 return (wantarray) ? @xhtml : join("\n", @xhtml);
678             }
679              
680 3     3 1 14 sub xhtml_hidden_field { shift->xhtml_hidden_fields(@_) }
681              
682             sub element
683             {
684 0     0 1 0 my($self) = shift;
685 0 0       0 Carp::croak "Cannot set element for ", ref($self) if(@_);
686 0         0 return $self->html_element;
687             }
688              
689             sub html_tag
690             {
691 943     943 1 3497 my($self) = shift;
692              
693 943 100 66     2392 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
694             {
695 2         20 $self->add_class('error');
696              
697 2         17 my $html = $self->Rose::HTML::Object::html_tag(@_);
698              
699 2         62 $self->delete_class('error');
700              
701 2         11 return $html;
702             }
703             else
704             {
705 941         2660 $self->SUPER::html_tag(@_);
706             }
707             }
708              
709             sub xhtml_tag
710             {
711 453     453 1 843 my($self) = shift;
712              
713 453 100 66     1482 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
714             {
715 2         22 $self->add_class('error');
716              
717 2         16 my $html = $self->Rose::HTML::Object::xhtml_tag(@_);
718              
719 2         16 $self->delete_class('error');
720              
721 2         8 return $html;
722             }
723             else
724             {
725 451         1339 $self->SUPER::xhtml_tag(@_);
726             }
727             }
728              
729             *html_field = \&html_tag;
730             *xhtml_field = \&xhtml_tag;
731              
732             sub html
733             {
734 50     50 1 257 my($self) = shift;
735              
736 50         95 my($field, $error);
737              
738 50         187 $field = $self->html_field;
739 50         391 $error = $self->html_error;
740              
741 50 100       175 if($error)
742             {
743 5         40 return $field . $self->html_error_separator . $error;
744             }
745              
746 45         360 return $field;
747             }
748              
749             sub xhtml
750             {
751 32     32 1 488 my($self) = shift;
752              
753 32         60 my($field, $error);
754              
755 32         123 $field = $self->xhtml_field;
756 32         174 $error = $self->xhtml_error;
757              
758 32 100       128 if($error)
759             {
760 4         83 return $field . $self->xhtml_error_separator . $error;
761             }
762              
763 28         176 return $field;
764             }
765              
766             # sub label
767             # {
768             #
769             # }
770              
771             sub label_object
772             {
773 406     406 1 724 my($self) = shift;
774 406   66     1257 my $label = $self->{'label_object'} ||= ref($self)->object_type_class_loaded('label')->new();
775              
776 406 100       1131 $label->contents($self->escape_html ? __escape_html($self->label) :
777             $self->label);
778              
779 406 100       1325 if($self->html_attr_exists('id'))
780             {
781 26         88 $label->for($self->html_attr('id'));
782             }
783              
784             $self->required ?
785 406 100       1090 $label->add_class('required') :
786             $label->delete_class('required');
787              
788 406 100       1868 defined $self->error ?
789             $label->add_class('error') :
790             $label->delete_class('error');
791              
792 406 50       1325 if(@_)
793             {
794 0         0 my %args = @_;
795              
796 0         0 while(my($k, $v) = each(%args))
797             {
798 0         0 $label->html_attr($k => $v);
799             }
800             }
801              
802 406         1317 return $label;
803             }
804              
805             sub html_label
806             {
807 400     400 1 816 my($self) = shift;
808 35     35   326 no warnings 'uninitialized';
  35         700  
  35         3388  
809 400 100       1104 return '' unless(length $self->label);
810 392         1143 return $self->label_object(@_)->html_tag;
811             }
812              
813             sub xhtml_label
814             {
815 25     25 1 60 my($self) = shift;
816 35     35   318 no warnings 'uninitialized';
  35         107  
  35         18131  
817 25 100       123 return '' unless(length $self->label);
818 11         47 return $self->label_object(@_)->xhtml_tag;
819             }
820              
821             sub validate
822             {
823 214     214 1 448 my($self) = shift;
824              
825 214         782 $self->error(undef);
826              
827 214         623 my $value = $self->internal_value;
828              
829 214 100 66     756 if($self->required &&
      66        
830             ((!ref $value && (!defined $value || ($self->trim_spaces && $value !~ /\S/))) ||
831             (ref $value eq 'ARRAY' && !@$value)))
832             {
833 21 50 33     358 unless($self->is_empty && $self->empty_is_ok)
834             {
835 21         478 my $label = $self->error_label;
836              
837 21 100       66 if(defined $label)
838             {
839             #$self->add_error_id(FIELD_REQUIRED, $label);
840             #$self->add_error_id(FIELD_REQUIRED, [ $label ]);
841 7         54 $self->add_error_id(FIELD_REQUIRED, { label => $label });
842             }
843             else
844             {
845 14         100 $self->add_error_id(FIELD_REQUIRED);
846             }
847              
848 21         91 return 0;
849             }
850             }
851              
852 193 100       1784 return $self->validate_with_validator($value) if($self->validator);
853              
854 191         504 return 1;
855             }
856              
857             sub validate_with_validator
858             {
859 2     2 0 6 my($self, $value) = @_;
860              
861 2 50       8 my $code = $self->validator or return 1;
862              
863 2         8 local $_ = $value;
864             #$Debug && warn "running $code->($self)\n";
865 2         8 my $ok = $code->($self);
866              
867 2 50 66     30 if(!$ok && !$self->has_errors)
868             {
869 1         16 my $label = $self->label;
870              
871 1 50       12 if(defined $label)
872             {
873 0         0 $self->add_error_id(FIELD_INVALID, { label => $label })
874             }
875             else
876             {
877 1         11 $self->add_error_id(FIELD_INVALID);
878             }
879             }
880              
881 2         8 return $ok;
882             }
883              
884             sub validator
885             {
886 214     214 1 419 my($self) = shift;
887              
888 214 100       473 if(@_)
889             {
890 2         7 my $code = shift;
891              
892 2 50       10 if(ref $code eq 'CODE')
893             {
894 2         9 return $self->{'validator'} = $code;
895             }
896             else
897             {
898 0         0 Carp::croak ref($self), "::validator() - argument must be a code reference";
899             }
900             }
901              
902 212         551 return $self->{'validator'};
903             }
904              
905             *__escape_html = \&Rose::HTML::Util::escape_html;
906              
907             sub message_for_error_id
908             {
909 90     90 1 473 my($self, %args) = @_;
910              
911 90         247 my $error_id = $args{'error_id'};
912 90   66     313 my $msg_class = $args{'msg_class'} || $self->localizer->message_class;
913 90   100     409 my $args = $args{'args'} || [];
914              
915 35     35   460 no warnings 'uninitialized';
  35         91  
  35         28520  
916 90 100       349 if($error_id == FIELD_REQUIRED)
    100          
917             {
918 25         130 my $msg = $msg_class->new(args => $args);
919              
920 25 100 66     352 if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
      33        
      66        
921             {
922 6         34 $msg->id(FIELD_REQUIRED_LABELLED);
923             }
924             else
925             {
926 19         96 $msg->id(FIELD_REQUIRED_GENERIC);
927             }
928              
929 25         101 return $msg;
930             }
931             elsif($error_id == FIELD_INVALID)
932             {
933 1         19 my $msg = $msg_class->new(args => $args);
934              
935 1 50 33     23 if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
      33        
      33        
936             {
937 0         0 $msg->id(FIELD_INVALID_LABELLED);
938             }
939             else
940             {
941 1         9 $msg->id(FIELD_INVALID_GENERIC);
942             }
943              
944 1         7 return $msg;
945             }
946              
947 64         261 return undef;
948             }
949              
950 1     1 0 19 sub localize_label { shift->label_message_id(FIELD_LABEL) }
951 1     1 0 10 sub localize_description { shift->description_message_id(FIELD_DESCRIPTION) }
952              
953             # XXX: Ths sub contains a lame hack to work around an incompatibility with
954             # XXX: Scalar::Defer 0.11 - it manually detects and evaluates code refs.
955             sub localizer
956             {
957 5474     5474 1 12492 my($invocant) = shift;
958              
959             # Called as object method
960 5474 100       11111 if(my $class = ref $invocant)
961             {
962 5330 50       10622 if(@_)
963             {
964 0         0 $invocant->{'localizer'} = shift;
965              
966 0 0       0 if(ref $invocant->{'localizer'} eq 'CODE')
967             {
968 0         0 return $invocant->{'localizer'}->();
969             }
970             else
971             {
972 0         0 return $invocant->{'localizer'};
973             }
974             }
975              
976 5330         8738 my $localizer = $invocant->{'localizer'};
977              
978 5330 50       9891 unless($localizer)
979             {
980 5330 100       10740 if(my $parent_field = $invocant->parent_field)
    100          
981             {
982 21 50       75 if(my $localizer = $parent_field->localizer)
983             {
984 21 50       1000 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
985             }
986             }
987             elsif(my $parent_form = $invocant->parent_form)
988             {
989 183 50       441 if(my $localizer = $parent_form->localizer)
990             {
991 183 50       7749 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
992             }
993             }
994 5126         14048 else { return $class->default_localizer }
995             }
996              
997 0 0       0 if(ref $localizer eq 'CODE')
998             {
999 0         0 return $localizer->();
1000             }
1001              
1002 0   0     0 return $localizer || $class->default_localizer;
1003             }
1004             else # Called as class method
1005             {
1006 144 100       517 if(@_)
1007             {
1008 1         12 return $invocant->default_localizer(shift);
1009             }
1010              
1011 143         927 return $invocant->default_localizer;
1012             }
1013             }
1014              
1015             # XXX: Ths sub contains a lame hack to work around an incompatibility with
1016             # XXX: Scalar::Defer 0.11 - it manually detects and evaluates code refs.
1017             sub locale
1018             {
1019 4095     4095 1 6446 my($invocant) = shift;
1020              
1021             # Called as object method
1022 4095 50       7834 if(my $class = ref $invocant)
1023             {
1024 4095 100       7828 if(@_)
1025             {
1026 20         54 $invocant->{'locale'} = shift;
1027              
1028 20 50       61 if(ref $invocant->{'locale'} eq 'CODE')
1029             {
1030 0         0 return $invocant->{'locale'}->();
1031             }
1032             else
1033             {
1034 20         55 return $invocant->{'locale'};
1035             }
1036             }
1037              
1038 4075         6290 my $locale = $invocant->{'locale'};
1039              
1040 4075 100       7527 if($locale)
1041             {
1042 48 50       200 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1043             }
1044              
1045 4027 100 66     8397 if(my $parent_field = $invocant->parent_field)
    100          
    100          
1046             {
1047 17 50       61 if(my $locale = $parent_field->locale)
1048             {
1049 17 50       83 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1050             }
1051             }
1052             elsif(my $parent_form = $invocant->parent_form)
1053             {
1054 312 50       757 if(my $locale = $parent_form->locale)
1055             {
1056 312 50       1252 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1057             }
1058             }
1059             elsif($invocant->can('parent_group') && (my $parent_group = $invocant->parent_group))
1060             {
1061 1609 50       3613 if(my $locale = $parent_group->locale)
1062             {
1063 1609 50       6113 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1064             }
1065             }
1066             else
1067             {
1068 2089         4131 my $locale = $invocant->localizer->locale;
1069              
1070 2089 50       94964 if(ref $locale eq 'CODE')
1071             {
1072 0         0 return $locale->();
1073             }
1074              
1075 2089   33     8050 return $locale || $class->default_locale;
1076             }
1077             }
1078             else # Called as class method
1079             {
1080 0 0         if(@_)
1081             {
1082 0           return $invocant->default_locale(shift);
1083             }
1084              
1085 0           return $invocant->default_locale;
1086             }
1087             }
1088              
1089       103 1   sub prepare { }
1090              
1091             if(__PACKAGE__->localizer->auto_load_messages)
1092             {
1093             __PACKAGE__->localizer->load_all_messages;
1094             }
1095              
1096 35     35   309 use utf8; # The __DATA__ section contains UTF-8 text
  35         85  
  35         430  
1097              
1098             1;
1099              
1100             __DATA__
1101              
1102             [% LOCALE en %]
1103              
1104             FIELD_LABEL = ""
1105             FIELD_DESCRIPTION = ""
1106             FIELD_REQUIRED_GENERIC = "This is a required field."
1107             FIELD_REQUIRED_LABELLED = "[1] is a required field."
1108              
1109             FIELD_PARTIAL_VALUE = "Incomplete value."
1110             FIELD_INVALID_GENERIC = "Value is invalid."
1111             FIELD_INVALID_LABELLED = "[label] is invalid."
1112              
1113             [% LOCALE de %]
1114              
1115             FIELD_REQUIRED_GENERIC = "Dies ist ein Pflichtfeld."
1116             FIELD_REQUIRED_LABELLED = "[1] ist ein Pflichtfeld."
1117              
1118             # ganze Sätze oder nur "Wert unvollständig/ungültig"?
1119             FIELD_PARTIAL_VALUE = "Der Wert ist unvollständig."
1120             FIELD_INVALID_GENERIC = "Der Wert ist ungültig."
1121             FIELD_INVALID_LABELLED = "[label] ist ungültig."
1122              
1123             [% LOCALE fr %]
1124              
1125             FIELD_REQUIRED_GENERIC = "Ce champ est obligatoire."
1126             FIELD_REQUIRED_LABELLED = "[1] est un champ obligatoire."
1127              
1128             FIELD_PARTIAL_VALUE = "Valeur incomplète."
1129             FIELD_INVALID_GENERIC = "Valeur invalide."
1130             FIELD_INVALID_LABELLED = "[label] est invalide."
1131              
1132             [% LOCALE bg %]
1133              
1134             FIELD_REQUIRED_GENERIC = "Това поле е задължително."
1135             FIELD_REQUIRED_LABELLED = "Полето '[1]' е задължително."
1136              
1137             FIELD_PARTIAL_VALUE = "Непълна стойност."
1138             FIELD_INVALID_GENERIC = "Стойността е невалидна."
1139             FIELD_INVALID_LABELLED = "Полето '[label]' е невалидно."
1140              
1141             __END__
1142              
1143             =head1 NAME
1144              
1145             Rose::HTML::Form::Field - HTML form field base class.
1146              
1147             =head1 SYNOPSIS
1148              
1149             package MyField;
1150              
1151             use base 'Rose::HTML::Form::Field';
1152             ...
1153              
1154             my $f = MyField->new(name => 'test', label => 'Test');
1155              
1156             print $f->html_field;
1157             print $f->xhtml_field;
1158              
1159             $f->input_value('hello world');
1160              
1161             $i = $f->internal_value;
1162              
1163             print $f->output_value;
1164             ...
1165              
1166             =head1 DESCRIPTION
1167              
1168             L<Rose::HTML::Form::Field> is the base class for field objects used in an HTML form. It defines a generic interface for field input, output, validation, and filtering.
1169              
1170             This class inherits from, and follows the conventions of, L<Rose::HTML::Object>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Object> documentation for more information.
1171              
1172             =head1 OVERVIEW
1173              
1174             A field object provides an interface for a logical field in an HTML form. Although it may serialize to multiple HTML tags, the field object itself is a single, logical entity.
1175              
1176             L<Rose::HTML::Form::Field> is the base class for field objects. Since the field object will eventually be asked to serialize itself as HTML, L<Rose::HTML::Form::Field> inherits from L<Rose::HTML::Object>. That defines a lot of a field object's interface, leaving only the field-specific functions to L<Rose::HTML::Form::Field> itself.
1177              
1178             The most important function of a field object is to accept and return user input. L<Rose::HTML::Form::Field> defines a data flow for field values with several different hooks and callbacks along the way:
1179              
1180             +------------+
1181             / user input /
1182             +------------+
1183             |
1184             V
1185             +------------------+
1186             set -->. .
1187             . input_value . input_value()
1188             get <--. .
1189             +------------------+
1190             |
1191             V
1192             +------------------+
1193             toggle -->| input_prefilter | trim_spaces()
1194             +------------------+
1195             |
1196             V
1197             +------------------+
1198             define <-->| input_filter | input_filter()
1199             +------------------+
1200             |
1201             V
1202             +----------------------+
1203             . .
1204             get <--. input_value_filtered . input_value_filtered()
1205             . .
1206             +----------------------+
1207             |
1208             V
1209             +------------------+
1210             | inflate_value | (override in subclass)
1211             +------------------+
1212             |
1213             V
1214             +------------------+
1215             . .
1216             get <--. internal_value . internal_value()
1217             . .
1218             +------------------+
1219             |
1220             V
1221             +------------------+
1222             | deflate_value | (override in subclass)
1223             +------------------+
1224             |
1225             V
1226             +------------------+
1227             define <-->| output_filter | output_filter()
1228             +------------------+
1229             |
1230             V
1231             +------------------+
1232             . .
1233             get <--. output_value . output_value()
1234             . .
1235             +------------------+
1236              
1237              
1238             Input must be done "at the top", by calling L<input_value()|/input_value>. The value as it exists at various stages of the flow can be retrieved, but it can only be set at the top. Input and output filters can be defined, but none exist by default.
1239              
1240             The purposes of the various stages of the data flow are as follows:
1241              
1242             =over 4
1243              
1244             =item B<input value>
1245              
1246             The value as it was passed to the field.
1247              
1248             =item B<input value filtered>
1249              
1250             The input value after being passed through all input filters, but before being inflated.
1251              
1252             =item B<internal value>
1253              
1254             The most useful representation of the value as far as the user of the L<Rose::HTML::Form::Field>-derived class is concerned. It has been filtered and optionally "inflated" into a richer representation (i.e., an object). The internal value must also be a valid input value.
1255              
1256             =item B<output value>
1257              
1258             The value as it will be used in the serialized HTML representation of the field, as well as in the equivalent URI query string. This is the internal value after being optionally "deflated" and then passed through an output filter. This value should be a string or a reference to an arry of strings. If passed back into the field as the input value, it should result in the same output value.
1259              
1260             =back
1261              
1262             Only subclasses can define class-wide "inflate" and "deflate" methods (by overriding the no-op implementations in this class), but users can define input and output filters on a per-object basis by passing code references to the appropriate object methods.
1263              
1264             The prefilter exists to handle common filtering tasks without hogging the lone input filter spot (or requiring users to constantly set input filters for every field). The L<Rose::HTML::Form::Field> prefilter optionally trims leading and trailing whitespace based on the value of the L<trim_spaces()|/trim_spaces> boolean attribute. This is part of the public API for field objects, so subclasses that override L<input_prefilter()|/input_prefilter> must preserve this functionality.
1265              
1266             In addition to the various kinds of field values, each field also has a name, which may or may not be the same as the value of the "name" HTML attribute.
1267              
1268             Fields also have associated labels, error strings, default values, and various methods for testing, clearing, and reseting the field value. See the list of object methods below for the details.
1269              
1270             =head1 HIERARCHY
1271              
1272             Though L<Rose::HTML::Form::Field> objects inherit from L<Rose::HTML::Object>, there are some semantic differences when it comes to the L<hierarchy|Rose::HTML::Object/HIERARCHY> of parent/child objects.
1273              
1274             A field is an abstraction for a collection of one or more HTML tags, including the field itself, the field's L<label|/html_label>, and any L<error messages|/html_error>. Each of these things may be made up of multiple HTML elements, and they usually exist alongside each other rather than nested within each other. As such, the field itself cannot rightly be considered the "parent" of these elements. This is why the child-related methods inherited from L<Rose::HTML::Object> (L<children|Rose::HTML::Object/children>, L<descendants|Rose::HTML::Object/descendants>, etc.) will usually return empty lists. Furthermore, any children L<added|Rose::HTML::Object/add_children> to the list will generally be ignored by the field's HTML output methods.
1275              
1276             Effectively, once we move away from the L<Rose::HTML::Object>-derived classes that represent a single HTML element (with zero or more children nested within it) to a class that presents a higher-level abstraction, such as a L<form|Rose::HTML::Form> or field, the exact population of and relationship between the constituent HTML elements may be opaque.
1277              
1278             If a field is a group of sibling HTML elements with no real parent HTML element (e.g., a L<radio button group|Rose::HTML::Form::Field::RadioButtonGroup>), then the individual sibling items will be available through a dedicated method (e.g., L<radio_buttons|Rose::HTML::Form::Field::RadioButtonGroup/radio_buttons>).
1279              
1280             In cases where there really is a clear parent/child relationship among the HTML elements that make up a field, such as a L<select box|Rose::HTML::Form::Field::SelectBox> which contains zero or more L<options|Rose::HTML::Form::Field::Option> or L<option groups|Rose::HTML::Form::Field::OptionGroup>, the L<children|Rose::HTML::Object/children> method will return the expected list of objects. In such cases, the list of child objects is usually restricted to be of the expected type (e.g., L<radio buttons|Rose::HTML::Form::Field::RadioButton> for a L<radio button group|Rose::HTML::Form::Field::RadioButtonGroup>), with all child-related methods acting as aliases for the existing item methods. For example, the L<add_options|Rose::HTML::Form::Field::SelectBox/add_options> method in L<Rose::HTML::Form::Field::SelectBox> does the same thing as L<add_children|Rose::HTML::Object/add_children>. See the documentation for each specific L<Rose::HTML::Form::Field>-derived class for more details.
1281              
1282             =head1 CUSTOM FIELDS
1283              
1284             This module distribution contains classes for most simple HTML fields, as well as examples of several more complex field types. These "custom" fields do things like accept only valid email addresses or dates, coerce input and output into fixed formats, and provide rich internal representations (e.g., L<DateTime> objects). Compound fields are made up of more than one field, and this construction can be nested: compound fields can contain other compound fields. So long as each custom field class complies with the API outlined here, it doesn't matter how complex it is internally (or externally, in its HTML serialization).
1285              
1286             (There are, however, certain rules that compound fields must follow in order to work correctly inside L<Rose::HTML::Form> objects. See the L<Rose::HTML::Form::Field::Compound> documentation for more information.)
1287              
1288             All of these classes are meant to be a starting point for your own custom fields. The custom fields included in this module distribution are mostly meant as examples of what can be done. I will accept any useful, interesting custom field classes into the C<Rose::HTML::Form::Field::*> namespace, but I'd also like to encourage suites of custom field classes in other namespaces entirely. Remember, subclassing doesn't necessarily dictate namespace.
1289              
1290             Building up a library of custom fields is almost always a big win in the long run. Reuse, reuse, reuse!
1291              
1292             =head1 HTML ATTRIBUTES
1293              
1294             L<Rose::HTML::Form::Field> has the following set of valid HTML attributes.
1295              
1296             accesskey
1297             class
1298             dir
1299             id
1300             lang
1301             name
1302             onblur
1303             onclick
1304             ondblclick
1305             onfocus
1306             onkeydown
1307             onkeypress
1308             onkeyup
1309             onmousedown
1310             onmousemove
1311             onmouseout
1312             onmouseover
1313             onmouseup
1314             style
1315             tabindex
1316             title
1317             value
1318             xml:lang
1319              
1320             =head1 CONSTRUCTOR
1321              
1322             =over 4
1323              
1324             =item B<new PARAMS>
1325              
1326             Constructs a new L<Rose::HTML::Form::Field> object based on PARAMS, where
1327             PARAMS are name/value pairs. Any object method is a valid parameter name.
1328              
1329             =back
1330              
1331             =head1 OBJECT METHODS
1332              
1333             =over 4
1334              
1335             =item B<auto_invalidate_parent [BOOL]>
1336              
1337             Get or set a boolean value that indicates whether or not the value of any parent field is automatically invalidated when the input value of this field is set. The default is true.
1338              
1339             See L</"parent_field"> and L</"invalidate_value"> for more information.
1340              
1341             =item B<clear>
1342              
1343             Clears the field by setting both the "value" HTML attribute and the input value to undef. Also sets the L<is_cleared()|/is_cleared> flag.
1344              
1345             =item B<default VALUE>
1346              
1347             Convenience wrapper for L<default_value()|/default_value>
1348              
1349             =item B<default_value VALUE>
1350              
1351             Set the default value for the field. In the absence of a defined input value, the default value is used as the input value.
1352              
1353             =item B<deflate_value VALUE>
1354              
1355             This method is meant to be overridden by a subclass. It should take VALUE and "deflate" it to a form that is a suitable for the output value: a string or reference to an array of strings. The implementation in L<Rose::HTML::Form::Field> simply returns VALUE unmodified.
1356              
1357             =item B<description [TEXT]>
1358              
1359             Get or set a text description of the field. This text is not currently used anywhere, but may be in the future. It may be useful as help text, but keep in mind that any such text should stay true to its intended purpose: a description of the field.
1360              
1361             Going too far off into the realm of generic help text is not a good idea since this text may be used elsewhere by this class or subclasses, and there it will be expected to be a description of the field rather than a description of how to fill out the field (e.g. "Command-click to make multiple selections") or any other sort of help text.
1362              
1363             It may also be useful for debugging.
1364              
1365             =item B<description_id [ID [, ARGS]]>
1366              
1367             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the description.
1368              
1369             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the description. When setting the message id, an optional ARGS hash reference should be passed if the L<localized text|Rose::HTML::Object::Message::Localizer/"LOCALIZED TEXT"> for the L<corresponding|/message_for_error_id> message contains any L<placeholders|Rose::HTML::Object::Message::Localizer/"LOCALIZED TEXT">.
1370              
1371             =item B<error_label [STRING]>
1372              
1373             Get or set the field label used when constructing error messages. For example, an error message might say "Value for [label] is too large." The error label will go in the place of the C<[label]> placeholder.
1374              
1375             If no error label is set, this method simply returns the L<label|/label>.
1376              
1377             =item B<error_label_id [ID [, ARGS]]>
1378              
1379             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the error label. When setting the message id, an optional ARGS hash reference should be passed if the L<localized text|Rose::HTML::Object::Message::Localizer/"LOCALIZED TEXT"> for the L<corresponding|/message_for_error_id> message contains any L<placeholders|Rose::HTML::Object::Message::Localizer/"LOCALIZED TEXT">.
1380              
1381             =item B<filter [CODE]>
1382              
1383             Sets both the input filter and output filter to CODE.
1384              
1385             =item B<hidden_field>
1386              
1387             Convenience wrapper for L<hidden_fields()|/hidden_fields>
1388              
1389             =item B<hidden_fields>
1390              
1391             Returns one or more L<Rose::HTML::Form::Field::Hidden> objects that represent the hidden fields needed to encode this field's value.
1392              
1393             =item B<html>
1394              
1395             Returns the HTML serialization of the field, along with the HTML error message, if any. The field and error HTML are joined by L<html_error_separator()|/html_error_separator>, which is "E<lt>brE<gt>\n" by default.
1396              
1397             =item B<html_error>
1398              
1399             Returns the error text, if any, as a snippet of HTML that looks like this:
1400              
1401             <span class="error">Error text goes here</span>
1402              
1403             If the L<escape_html|Rose::HTML::Object/escape_html> flag is set to true (the default), then the error text has any HTML in it escaped.
1404              
1405             =item B<html_error_separator [STRING]>
1406              
1407             Get or set the string used to join the HTML field and HTML error message in the output of the L<html()|/html> method. The default value is "E<lt>brE<gt>\n"
1408              
1409             =item B<html_field>
1410              
1411             Returns the HTML serialization of the field.
1412              
1413             =item B<html_hidden_field>
1414              
1415             Convenience wrapper for L<html_hidden_fields()|/html_hidden_fields>
1416              
1417             =item B<html_hidden_fields>
1418              
1419             In scalar context, returns the HTML serialization of the fields returned by L<hidden_fields()|/hidden_fields>, joined by newlines. In list context, returns a list containing the HTML serialization of the fields returned by L<hidden_fields()|/hidden_fields>.
1420              
1421             =item B<html_label [ARGS]>
1422              
1423             Returns the HTML serialization of the label object, or the empty string if the field's C<label> is undefined or zero in length. Any ARGS are passed to the call to L<label_object()|/label_object>.
1424              
1425             If L<required()|/required>is true for this field, then the name/value pair "class => 'required'" is passed to the call to L<label_object()|/label_object> I<before> any arguments that you pass. This allows you to override the "class" value with one of your own.
1426              
1427             =item B<html_prefix [STRING]>
1428              
1429             Get or set an HTML prefix that may be displayed before the HTML field. L<Rose::HTML::Form::Field> does not use this prefix, but subclasses might. The default value is an empty string.
1430              
1431             =item B<html_suffix [STRING]>
1432              
1433             Get or set an HTML suffix that may be appended to the HTML field. L<Rose::HTML::Form::Field> does not use this suffix, but subclasses might. The default value is an empty string.
1434              
1435             =item B<html_tag>
1436              
1437             This method is part of the L<Rose::HTML::Object> API. In this case, it simply calls L<html_field()|/html_field>.
1438              
1439             =item B<inflate_value VALUE>
1440              
1441             This method is meant to be overridden by subclasses. It should take VALUE and "inflate" it to a form that is a suitable internal value. (See the L<OVERVIEW> for more on internal values.) The default implementation simply returns its first argument unmodified.
1442              
1443             =item B<input_filter [CODE]>
1444              
1445             Get or set the input filter.
1446              
1447             =item B<input_prefilter VALUE>
1448              
1449             Runs VALUE through the input prefilter. This method is called automatically when needed and is not meant to be called by users of this module. Subclasses may want to override it, however.
1450              
1451             The default implementation optionally trims leading and trailing spaces based on the value of the L<trim_spaces()|/trim_spaces> boolean attribute. This is part of the public API for field objects, so subclasses that override L<input_prefilter()|/input_prefilter> must preserve this functionality.
1452              
1453             =item B<input_value [VALUE]>
1454              
1455             Get or set the input value.
1456              
1457             =item B<input_value_filtered>
1458              
1459             Returns the input value after passing it through the input prefilter and input filter (if any).
1460              
1461             =item B<internal_value>
1462              
1463             Returns the internal value.
1464              
1465             =item B<invalidate_output_value>
1466              
1467             Invalidates the field's output value, causing it to be regenerated the next time it is retrieved. This method is useful if the output value is created based on some configurable attribute of the field (e.g., a delimiter string). If such an attribute is changed, then any existing output value must be invalidated.
1468              
1469             =item B<invalidate_value>
1470              
1471             Invalidates the field's value, causing the internal and output values to be recreated the next time they are retrieved.
1472              
1473             This method is most useful in conjunction with the L</"parent_field"> attribute. For example, when the input value of a subfield of a L<compound field|Rose::HTML::Form::Field::Compound> is set directly, it will invalidate the value of its parent field(s).
1474              
1475             =item B<is_cleared>
1476              
1477             Returns true if the field is cleared (i.e., if L<clear()|/clear> has been called on it and it has not subsequently been L<reset()|/reset> or given a new input value), false otherwise.
1478              
1479             =item B<is_empty>
1480              
1481             Returns false if the internal value contains any non-whitespace characters or if L<trim_spaces|/trim_spaces> is false and the internal value has a non-zero length, true otherwise. Subclasses should be sure to override this if they use internal values other than strings.
1482              
1483             =item B<is_full>
1484              
1485             Returns true if the internal value contains any non-whitespace characters, false otherwise. Subclasses should be sure to override this if they use internal values other than strings.
1486              
1487             =item B<label [STRING]>
1488              
1489             Get or set the field label. This label is used by the various label printing methods as well as in some error messages (assuming there is no explicitly defined L<error_label|/error_label>. Even if you don't plan to use any of the former, it might be a good idea to set it to a sensible value for use in the latter.
1490              
1491             =item B<label_id [ID]>
1492              
1493             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the field label.
1494              
1495             =item B<label_object [ARGS]>
1496              
1497             Returns a L<Rose::HTML::Label> object with its C<for> HTML attribute set to the calling field's C<id> attribute and any other HTML attributes specified by the name/value pairs in ARGS. The HTML contents of the label object are set to the field's L<label()|/label>, which has its HTML escaped if L<escape_html|Rose::HTML::Object/escape_html> is true (which is the default).
1498              
1499             =item B<local_name [NAME]>
1500              
1501             Get or set the name of this field from the perspective of the L<parent_form|/parent_form> or L<parent_field|/parent_field>, depending on which type of thing is the direct parent of this field. The local name should not change, regardless of how deeply this field is nested within other forms or fields.
1502              
1503             =item B<message_for_error_id PARAMS>
1504              
1505             Return the appropriate L<message|Rose::HTML::Object::Message> object associated with the L<error|Rose::HTML::Object::Errors> id. The error id, message class, and message placeholder values are specified by PARAMS name/value pairs. Valid PARAMS are:
1506              
1507             =over 4
1508              
1509             =item B<msg_class CLASS>
1510              
1511             The name of the L<Rose::HTML::Object::Message>-derived class used to store each message. If omitted, it defaults to the L<localizer|Rose::HTML::Object/localizer>'s L<message_class|Rose::HTML::Object::Message::Localizer/message_class>.
1512              
1513             =back
1514              
1515             =item B<name [NAME]>
1516              
1517             If passed a NAME argument, then the L<local_name|/local_name> is set to NAME and the "name" HTML attribute is set to the fully-qualified field name, which may include dot (".") separated prefixes for the L<parent forms|/parent_form> and/or L<parent fields|/parent_field>.
1518              
1519             If called without any arguments, and if the "name" HTML attribute is empty, then the "name" HTML attribute is set to the fully-qualified field name.
1520              
1521             Returns the value of the "name" HTML attribute.
1522              
1523             =item B<output_filter [CODE]>
1524              
1525             Get or set the output filter.
1526              
1527             =item B<output_value>
1528              
1529             Returns the output value.
1530              
1531             =item B<parent_field [FIELD]>
1532              
1533             Get or set the parent field. The parent field should only be set if the direct parent of this field is another field. The reference to the parent field is "weakened" using L<Scalar::Util::weaken()|Scalar::Util/weaken> in order to avoid memory leaks caused by circular references.
1534              
1535             =item B<parent_form [FORM]>
1536              
1537             Get or set the parent L<form|Rose::HTML::Form>. The parent form should only be set if the direct parent of this field is a form. The reference to the parent form is "weakened" using L<Scalar::Util::weaken()|Scalar::Util/weaken> in order to avoid memory leaks caused by circular references.
1538              
1539             =item B<parent_group [GROUP]>
1540              
1541             Get or set the parent group. Group objects are things like L<Rose::HTML::Form::Field::RadioButtonGroup> and L<Rose::HTML::Form::Field::CheckboxGroup>: conceptual groupings that have no concrete incarnation in HTML. (That is, there is no parent HTML tag wrapping checkbox or radio button groups.)
1542              
1543             The parent group should only be set if the direct parent of this field is a group object. The reference to the parent group is "weakened" using L<Scalar::Util::weaken()|Scalar::Util/weaken> in order to avoid memory leaks caused by circular references.
1544              
1545             =item B<prepare>
1546              
1547             Prepares the field for use in a form. Override this method in your custom field subclass to do any work required for each field before each use of that field. Be sure to call the superclass implementation as well. Example:
1548              
1549             package MyField;
1550             use base 'Rose::HTML::Form::Field';
1551             ...
1552             sub prepare
1553             {
1554             my($self) = shift;
1555              
1556             # Do anything that needs to be done before each use of this field
1557             ...
1558              
1559             # Call superclass implementation
1560             $self->SUPER::prepare(@_);
1561             }
1562              
1563             =item B<rank [INT]>
1564              
1565             Get or set the field's rank. This value can be used for any purpose that suits you, but it is most often used to number and sort fields within a L<form|Rose::HTML::Form> using a custom L<compare_fields()|Rose::HTML::Form/compare_fields> method.
1566              
1567             =item B<required [BOOL]>
1568              
1569             Get or set a boolean flag that indicates whether or not a field is "required." See L<validate()|/validate> for more on what "required" means.
1570              
1571             =item B<reset>
1572              
1573             Reset the field to its default state: the input value and L<error()|Rose::HTML::Object/error> are set to undef and the L<is_cleared()|/is_cleared> flag is set to false.
1574              
1575             =item B<trim_spaces [BOOL]>
1576              
1577             Get or set the boolean flag that indicates whether or not leading and trailing spaces should be removed from the field value in the input prefilter. The default is true.
1578              
1579             =item B<validate>
1580              
1581             Validate the field and return a true value if it is valid, false otherwise. If the field is C<required>, then its internal value is tested according to the following rules.
1582              
1583             * If the internal value is undefined, then return false.
1584              
1585             * If the internal value is a reference to an array, and the array is empty, then return false.
1586              
1587             * If L<trim_spaces()|/trim_spaces> is true (the default) and if the internal value does not contain any non-whitespace characters, return false.
1588              
1589             If false is returned due to one of the conditions above, then L<error()|Rose::HTML::Object/error> is set to the string:
1590              
1591             $label is a required field.
1592              
1593             where C<$label> is either the field's L<label()|/label> or, if L<label()|/label> is not defined, the string "This".
1594              
1595             If a custom L<validator()|/validator> is set, then C<$_> is localized and set to the internal value and the validator subroutine is called with the field object as the first and only argument.
1596              
1597             If the validator subroutine returns false and did not set L<error()|Rose::HTML::Object/error> to a defined value, then L<error()|Rose::HTML::Object/error> is set to the string:
1598              
1599             $label is invalid.
1600              
1601             where C<$label> is is either the field's L<label()|/label> or, if L<label()|/label> is not defined, the string "Value".
1602              
1603             The return value of the validator subroutine is then returned.
1604              
1605             If none of the above tests caused a value to be returned, then true is returned.
1606              
1607             =item B<validator [CODE]>
1608              
1609             Get or set a validator subroutine. If defined, this subroutine is called by L<validate()|/validate>.
1610              
1611             =item B<value [VALUE]>
1612              
1613             If a VALUE argument is passed, it sets both the input value and the "value" HTML attribute to VALUE. Returns the value of the "value" HTML attribute.
1614              
1615             =item B<xhtml>
1616              
1617             Returns the XHTML serialization of the field, along with the HTML error message, if any. The field and error HTML are joined by L<xhtml_error_separator()|/xhtml_error_separator>, which is "E<lt>br /E<gt>\n" by default.
1618              
1619             =item B<xhtml_error>
1620              
1621             Returns the error text, if any, as a snippet of XHTML that looks like this:
1622              
1623             <span class="error">Error text goes here</span>
1624              
1625             If the L<escape_html|Rose::HTML::Object/escape_html> flag is set to true (the default), then the error text has any HTML in it escaped.
1626              
1627             =item B<xhtml_error_separator [STRING]>
1628              
1629             Get or set the string used to join the XHTML field and HTML error message in the output of the L<xhtml()|/xhtml> method. The default value is "E<lt>br /E<gt>\n"
1630              
1631             =item B<xhtml_field>
1632              
1633             Returns the XHTML serialization of the field.
1634              
1635             =item B<xhtml_hidden_field>
1636              
1637             Convenience wrapper for L<xhtml_hidden_fields()|/xhtml_hidden_fields>
1638              
1639             =item B<xhtml_hidden_fields>
1640              
1641             In scalar context, returns the XHTML serialization of the fields returned by L<hidden_fields()|/hidden_fields>, joined by newlines. In list context, returns a list containing the XHTML serialization of the fields returned by L<hidden_fields()|/hidden_fields>.
1642              
1643             =item B<xhtml_label [ARGS]>
1644              
1645             Returns the XHTML serialization of the label object, or the empty string if the field's C<label> is undefined or zero in length. Any ARGS are passed to the call to L<label_object()|/label_object>.
1646              
1647             If L<required()|/required>is true for this field, then the name/value pair "class => 'required'" is passed to the call to L<label_object()|/label_object> I<before> any arguments that you pass. This allows you to override the "class" value with one of your own.
1648              
1649             =item B<xhtml_tag>
1650              
1651             This method is part of the L<Rose::HTML::Object> API. In this case, it simply calls L<xhtml_field()|/xhtml_field>.
1652              
1653             =back
1654              
1655             =head1 SUPPORT
1656              
1657             Any L<Rose::HTML::Objects> questions or problems can be posted to the L<Rose::HTML::Objects> mailing list. To subscribe to the list or search the archives, go here:
1658              
1659             L<http://groups.google.com/group/rose-html-objects>
1660              
1661             Although the mailing list is the preferred support mechanism, you can also email the author (see below) or file bugs using the CPAN bug tracking system:
1662              
1663             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-HTML-Objects>
1664              
1665             There's also a wiki and other resources linked from the Rose project home page:
1666              
1667             L<http://rosecode.org>
1668              
1669             =head1 AUTHOR
1670              
1671             John C. Siracusa (siracusa@gmail.com)
1672              
1673             =head1 LICENSE
1674              
1675             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.