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   105563 use strict;
  35         114  
  35         1143  
4              
5 35     35   195 use Carp();
  35         70  
  35         482  
6 35     35   206 use Scalar::Util();
  35         68  
  35         538  
7              
8 35     35   10783 use Rose::HTML::Util();
  35         113  
  35         852  
9              
10 35     35   14976 use Rose::HTML::Label;
  35         129  
  35         238  
11 35     35   247 use Rose::HTML::Object::Errors qw(:field);
  35         110  
  35         326  
12 35     35   244 use Rose::HTML::Object::Messages qw(:field);
  35         120  
  35         193  
13              
14 35     35   280 use base 'Rose::HTML::Object';
  35         108  
  35         4320  
15              
16 35     35   305 use constant HTML_ERROR_SEP => "<br>\n";
  35         88  
  35         3230  
17 35     35   266 use constant XHTML_ERROR_SEP => "<br />\n";
  35         70  
  35         2129  
18              
19 35     35   14456 use Rose::HTML::Form::Constants qw(FF_SEPARATOR);
  35         94  
  35         3179  
20              
21             our $VERSION = '0.617';
22              
23             #our $Debug = 0;
24              
25             use Rose::HTML::Object::MakeMethods::Localization
26             (
27 35         375 localized_message =>
28             [
29             qw(_error_label label description)
30             ],
31 35     35   279 );
  35         94  
32              
33             use Rose::Object::MakeMethods::Generic
34             (
35 35         609 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   3366 );
  35         107  
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 51 sub is_button { 0 }
62              
63             *label_id = \&label_message_id;
64              
65             sub error_label
66             {
67 61     61 1 122 my($self) = shift;
68              
69 61 100       152 if(@_)
70             {
71 2         11 return $self->_error_label(@_);
72             }
73              
74 59         242 my $label = $self->_error_label;
75              
76 35     35   5664 no warnings 'uninitialized';
  35         89  
  35         5132  
77 59 100       246 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    100          
78             }
79              
80             sub error_label_id
81             {
82 128     128 1 866 my($self) = shift;
83              
84 128 50       308 if(@_)
85             {
86 128         441 return $self->_error_label_message_id(@_);
87             }
88              
89 0         0 my $label = $self->_error_label_message_id;
90              
91 35     35   340 no warnings 'uninitialized';
  35         98  
  35         36416  
92 0 0       0 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    0          
93             }
94              
95 301     301 0 3499 sub init_apply_error_class { 1 }
96              
97             sub auto_invalidate_parent
98             {
99 3182     3182 1 5197 my($self) = shift;
100              
101 3182 50       5935 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       9332 ($self->{'auto_invalidate_parent'} = 1);
109             }
110              
111             sub invalidate_value
112             {
113 89     89 1 153 $_[0]->{'input_value'} = undef;
114 89         150 $_[0]->{'internal_value'} = undef;
115 89         226 $_[0]->{'output_value'} = undef;
116             }
117              
118             sub invalidate_output_value
119             {
120 1     1 1 3 $_[0]->{'output_value'} = undef;
121             }
122              
123             sub parent_group
124             {
125 5327     5327 1 8309 my($self) = shift;
126              
127 5327 100       10023 if(@_)
128             {
129 249 50       577 if(ref $_[0])
130             {
131 249         1156 Scalar::Util::weaken($self->{'parent_group'} = shift);
132 249         650 return $self->{'parent_group'};
133             }
134             else
135             {
136 0         0 return $self->{'parent_group'} = shift;
137             }
138             }
139              
140 5078         14973 return $self->{'parent_group'};
141             }
142              
143             sub parent_field
144             {
145 33306     33306 1 48336 my($self) = shift;
146              
147 33306 100       60486 if(@_)
148             {
149 317 50       696 if(ref $_[0])
150             {
151 317         1265 Scalar::Util::weaken($self->{'parent_field'} = shift);
152 317         865 return $self->{'parent_field'};
153             }
154             else
155             {
156 0         0 return $self->{'parent_field'} = shift;
157             }
158             }
159              
160 32989         87614 return $self->{'parent_field'};
161             }
162              
163             sub parent_form
164             {
165 40097     40097 1 57209 my($self) = shift;
166              
167 40097 100       72518 if(@_)
168             {
169 329 50       873 if(ref $_[0])
170             {
171 329         1398 Scalar::Util::weaken($self->{'parent_form'} = shift);
172 329         713 return $self->{'parent_form'};
173             }
174             else
175             {
176 0         0 return $self->{'parent_form'} = shift;
177             }
178             }
179              
180 39768         101695 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 12274     12274 0 17724 my($self) = shift;
200 12274         20705 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  36822         83474  
201             $self->field_context_name,
202             $self->local_name);
203             }
204              
205             sub fq_moniker
206             {
207 1804     1804 0 2835 my($self) = shift;
208              
209 1804         3260 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  5412         21070  
210             $self->field_context_name,
211             $self->local_moniker);
212             }
213              
214 44     44 0 471 sub init_local_moniker { shift->local_name }
215              
216             sub form_context_name
217             {
218 15439     15439 0 21840 my($self) = shift;
219 15439 100       25919 my $parent_form = $self->parent_form or return undef;
220 6217         14557 return $parent_form->fq_form_name;
221             }
222              
223             sub field_context_name
224             {
225 15439     15439 0 24247 my($self) = shift;
226 15439 100       27344 my $parent_field = $self->parent_field or return undef;
227 6301 50       11805 my $name = $parent_field->fq_name or return undef;
228 6301         14552 return $name;
229             }
230              
231 11     11 0 26 sub is_flat_group { 0 }
232              
233 32     32 0 373 sub init_html_prefix { '' }
234 32     32 0 499 sub init_html_suffix { '' }
235              
236 5     5 0 104 sub init_html_error_separator { HTML_ERROR_SEP }
237 4     4 0 81 sub init_xhtml_error_separator { XHTML_ERROR_SEP }
238              
239             sub value
240             {
241 167     167 1 926 my($self) = shift;
242              
243 167 50       541 if(@_)
244             {
245 167         445 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 3355     3355 0 5625 my($self) = shift;
256              
257 3355         9917 $self->html_attr('name', undef);
258 3355 50 66     7100 $self->name if($self->parent_field || $self->parent_form);
259             #$self->name($self->fq_name);
260             }
261              
262             sub local_name
263             {
264 15011     15011 1 23107 my($self) = shift;
265              
266 15011 100       27684 if(@_)
267             {
268 1546         2472 my $name = shift;
269              
270 35     35   305 no warnings 'uninitialized';
  35         93  
  35         10097  
271 1546 50 66     4514 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 1546         2925 my $old_name = $self->{'local_name'};
277 1546         2656 $self->{'local_name'} = $name;
278              
279 1546 100 100     4837 if(defined $old_name && $name ne $old_name)
280             {
281 3 100       12 if(my $parent_form = $self->parent_form)
282             {
283 1         13 $parent_form->delete_field($old_name);
284 1         13 $parent_form->add_field($name => $self);
285             }
286              
287 3 50       12 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 1546         3071 return $name;
295             }
296              
297 13465         21672 my $name = $self->{'local_name'};
298 13465 100       31566 return $name if(defined $name);
299 452         1286 return $self->{'local_name'} = $self->{'local_moniker'};
300             }
301              
302             sub name
303             {
304 8339     8339 1 16439 my($self) = shift;
305              
306 8339 100       16069 if(@_)
307             {
308 559         1645 $self->local_name(shift);
309 559         1613 return $self->html_attr('name', $self->fq_name);
310             }
311              
312 7780         16490 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   319 no warnings 'uninitialized';
  35         94  
  35         37575  
317 7780 100       15962 unless(length $name)
318             {
319 2702         5945 return $self->html_attr('name', $self->fq_name);
320             }
321              
322 5078         13959 return $name;
323             }
324              
325             sub moniker
326             {
327 758     758 0 1374 my($self) = shift;
328              
329 758 100       1531 if(@_)
330             {
331 257         902 return $self->fq_moniker($self->{'moniker'} = shift);
332             }
333             else
334             {
335 501 100       1688 return $self->{'moniker'} if(defined $self->{'moniker'});
336 57         190 return $self->{'moniker'} = $self->fq_moniker;
337             }
338             }
339              
340             sub default_value
341             {
342 4829     4829 1 7328 my($self) = shift;
343              
344 4829 100       8888 if(@_)
345             {
346 554         1015 $self->{'internal_value'} = undef;
347 554         915 $self->{'output_value'} = undef;
348 554         1583 return $self->{'default_value'} = shift;
349             }
350              
351 4275         9090 return $self->{'default_value'};
352             }
353              
354 30     30 1 322 sub default { shift->default_value(@_) }
355              
356 1548     1548 1 2661 sub inflate_value { $_[1] }
357 252     252 1 525 sub deflate_value { $_[1] }
358              
359             sub input_value
360             {
361 5942     5942 1 26575 my($self) = shift;
362              
363 5942 100       10998 if(@_)
364             {
365 2921         4668 $self->{'is_cleared'} = 0;
366 2921         4557 $self->{'internal_value'} = undef;
367 2921         4477 $self->{'output_value'} = undef;
368 2921         4549 $self->{'errors'} = undef;
369 2921         4832 $self->{'input_value'} = shift;
370              
371 2921 100       5725 if(my $parent = $self->parent_field)
372             {
373 1211 100 100     3177 $parent->is_cleared(0) if(!$parent->{'in_init'} && $parent->_is_full);
374              
375 1211 100       2865 if($self->auto_invalidate_parent)
376             {
377 54         228 $parent->invalidate_value;
378             }
379             }
380              
381 2921         7388 return $self->{'input_value'};
382             }
383              
384 3021 100 100     6071 return undef if($self->is_cleared || $self->has_partial_value);
385              
386             my $value =
387 2952 100       31201 (defined $self->{'input_value'}) ? $self->{'input_value'} :
388             $self->default_value;
389              
390 2952 50 33     6323 if(wantarray && ref $value eq 'ARRAY')
391             {
392 0         0 return @$value;
393             }
394              
395 2952         5747 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   5172 local $_[0]->{'auto_invalidate_parent'} = 0;
403 2462         5333 shift->input_value(@_);
404             }
405              
406             sub _is_full
407             {
408 1314     1314   2040 my($self) = shift;
409              
410 1314 100       3558 if($self->is_full(@_))
411             {
412 205         720 $self->has_partial_value(0);
413 205         1443 return 1;
414             }
415              
416 1109         4392 return 0;
417             }
418              
419             sub input_value_filtered
420             {
421 78     78 1 161 my($self) = shift;
422              
423 78         188 my $value = $self->input_value;
424              
425 78         219 $value = $self->input_prefilter($value);
426              
427 78 100       177 if(my $input_filter = $self->input_filter)
428             {
429 4         13 local $_ = $value;
430 4         14 $value = $input_filter->($self, $value);
431             }
432              
433 78         323 return $value;
434             }
435              
436             sub internal_value
437             {
438 7302     7302 1 13102 my($self) = shift;
439              
440 7302 50       13729 Carp::croak "Cannot set the internal value. Use input_value() instead." if(@_);
441              
442 7302 100 100     14014 return undef if($self->is_cleared || $self->has_partial_value);
443              
444 3320 100       34884 if(defined $self->{'internal_value'})
445             {
446 1276 100 100     3318 if(wantarray && ref $self->{'internal_value'} eq 'ARRAY')
447             {
448 2         6 return @{$self->{'internal_value'}};
  2         11  
449             }
450              
451 1274         3771 return $self->{'internal_value'};
452             }
453              
454 2044         4628 my $value = $self->input_value;
455              
456 2044         3298 my($using_default, $final_value);
457              
458 2044 100       4000 unless(defined $value)
459             {
460 1361         2339 $value = $self->default_value;
461 1361         2244 $using_default++;
462             }
463              
464 2044         4336 $value = $self->input_prefilter($value);
465              
466 2044 100       4235 if(my $input_filter = $self->input_filter)
467             {
468 29         60 local $_ = $value;
469 29         75 $final_value = $input_filter->($self, $value);
470             }
471 2015         3062 else { $final_value = $value }
472              
473 2044         4845 $final_value = $self->inflate_value($final_value);
474              
475 2044 100       4402 $self->{'internal_value'} = $final_value unless($using_default);
476              
477 2044 100 100     5379 if(wantarray && ref $final_value eq 'ARRAY')
478             {
479 2         9 return @$final_value;
480             }
481              
482 2042         5922 return $final_value;
483             }
484              
485 0     0 0 0 sub internal_value_singular { shift->internal_value(@_) }
486              
487             sub output_value
488             {
489 548     548 1 11209 my($self) = shift;
490              
491 548 50       1282 Carp::croak "Cannot set the output value. Use input_value() instead." if(@_);
492              
493 548 100       1548 return undef if($self->is_cleared);
494              
495 492 100       3720 return $self->{'output_value'} if(defined $self->{'output_value'});
496              
497 320         789 my $value = $self->deflate_value(scalar $self->internal_value);
498              
499 320 100       5135 if(my $output_filter = $self->output_filter)
500             {
501 22         47 local $_ = $value;
502 22         65 $self->{'output_value'} = $output_filter->($self, $value);
503             }
504 298         559 else { $self->{'output_value'} = $value }
505              
506 320 50 66     1417 if(wantarray && ref $self->{'output_value'} eq 'ARRAY')
507             {
508 0         0 return @{$self->{'output_value'}};
  0         0  
509             }
510              
511 320         1368 return $self->{'output_value'};
512             }
513              
514             sub is_empty
515             {
516 3607     3607 1 5478 my($self) = shift;
517              
518 3607         6028 my $value = $self->internal_value;
519              
520 35     35   302 no warnings;
  35         84  
  35         5155  
521 3607 100 66     21165 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 2122     2122 1 3600 my($self, $value) = @_;
529              
530 2122 100       4467 return undef unless(defined $value);
531              
532 737 100       1626 unless(ref $value)
533             {
534 700         1367 for($value)
535             {
536 35     35   334 no warnings;
  35         103  
  35         16015  
537              
538 700 100       1722 if($self->trim_spaces)
539             {
540 698         5471 s/^\s+//;
541 698         2150 s/\s+$//;
542             }
543             }
544             }
545              
546 737         1704 return $value;
547             }
548              
549             sub input_filter
550             {
551 2133     2133 1 4063 my($self) = shift;
552              
553 2133 100       4470 if(@_)
554             {
555 11         26 $self->{'internal_value'} = undef;
556 11         29 $self->{'output_value'} = undef;
557 11         39 return $self->{'input_filter'} = shift;
558             }
559              
560 2122         4668 return $self->{'input_filter'};
561             }
562              
563             sub output_filter
564             {
565 335     335 1 3371 my($self) = shift;
566              
567 335 100       757 if(@_)
568             {
569 14         43 $self->{'output_value'} = undef;
570 14         59 return $self->{'output_filter'} = shift;
571             }
572              
573 321         789 return $self->{'output_filter'};
574             }
575              
576             sub filter
577             {
578 1     1 1 2 my($self) = shift;
579              
580 1 50       5 if(@_)
581             {
582 1         2 my $filter = shift;
583 1         7 $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 3536 my($self) = shift;
600              
601 1808         4211 $self->_set_input_value(undef);
602 1808         5382 $self->error(undef);
603 1808         4722 $self->has_partial_value(0);
604 1808         7740 $self->is_cleared(1);
605             }
606              
607             sub reset
608             {
609 66     66 1 145 my($self) = shift;
610              
611 66         192 $self->_set_input_value(undef);
612 66         335 $self->error(undef);
613 66         228 $self->has_partial_value(0);
614 66         336 $self->is_cleared(0);
615 66         281 return 1;
616             }
617              
618             sub hidden_fields
619             {
620 115     115 1 904 my($self) = shift;
621              
622 115         421 my $hidden_field_class = ref($self)->object_type_class_loaded('hidden');
623              
624 35     35   305 no strict 'refs';
  35         98  
  35         33585  
625 115 50       231 unless(@{$hidden_field_class . '::ISA'})
  115         580  
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         332 $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 284 sub hidden_field { shift->hidden_fields(@_) }
649              
650             sub html_hidden_fields
651             {
652 12     12 1 34 my($self) = shift;
653              
654 12         27 my @html;
655              
656 12         62 foreach my $field ($self->hidden_fields)
657             {
658 18         63 push(@html, $field->html_field);
659             }
660              
661 12 50       198 return (wantarray) ? @html : join("\n", @html);
662             }
663              
664 8     8 1 57 sub html_hidden_field { shift->html_hidden_fields(@_) }
665              
666             sub xhtml_hidden_fields
667             {
668 7     7 1 21 my($self) = shift;
669              
670 7         15 my @xhtml;
671              
672 7         27 foreach my $field ($self->hidden_fields)
673             {
674 9         38 push(@xhtml, $field->xhtml_field);
675             }
676              
677 7 50       93 return (wantarray) ? @xhtml : join("\n", @xhtml);
678             }
679              
680 3     3 1 17 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 940     940 1 3806 my($self) = shift;
692              
693 940 100 66     2495 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
694             {
695 2         16 $self->add_class('error');
696              
697 2         24 my $html = $self->Rose::HTML::Object::html_tag(@_);
698              
699 2         27 $self->delete_class('error');
700              
701 2         10 return $html;
702             }
703             else
704             {
705 938         2756 $self->SUPER::html_tag(@_);
706             }
707             }
708              
709             sub xhtml_tag
710             {
711 450     450 1 827 my($self) = shift;
712              
713 450 100 66     1161 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
714             {
715 2         11 $self->add_class('error');
716              
717 2         13 my $html = $self->Rose::HTML::Object::xhtml_tag(@_);
718              
719 2         23 $self->delete_class('error');
720              
721 2         7 return $html;
722             }
723             else
724             {
725 448         1377 $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 225 my($self) = shift;
735              
736 50         132 my($field, $error);
737              
738 50         184 $field = $self->html_field;
739 50         374 $error = $self->html_error;
740              
741 50 100       161 if($error)
742             {
743 5         44 return $field . $self->html_error_separator . $error;
744             }
745              
746 45         326 return $field;
747             }
748              
749             sub xhtml
750             {
751 32     32 1 528 my($self) = shift;
752              
753 32         63 my($field, $error);
754              
755 32         106 $field = $self->xhtml_field;
756 32         174 $error = $self->xhtml_error;
757              
758 32 100       97 if($error)
759             {
760 4         86 return $field . $self->xhtml_error_separator . $error;
761             }
762              
763 28         152 return $field;
764             }
765              
766             # sub label
767             # {
768             #
769             # }
770              
771             sub label_object
772             {
773 406     406 1 646 my($self) = shift;
774 406   66     1212 my $label = $self->{'label_object'} ||= ref($self)->object_type_class_loaded('label')->new();
775              
776 406 100       1130 $label->contents($self->escape_html ? __escape_html($self->label) :
777             $self->label);
778              
779 406 100       1359 if($self->html_attr_exists('id'))
780             {
781 26         91 $label->for($self->html_attr('id'));
782             }
783              
784             $self->required ?
785 406 100       1063 $label->add_class('required') :
786             $label->delete_class('required');
787              
788 406 100       1871 defined $self->error ?
789             $label->add_class('error') :
790             $label->delete_class('error');
791              
792 406 50       1364 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         1314 return $label;
803             }
804              
805             sub html_label
806             {
807 400     400 1 838 my($self) = shift;
808 35     35   293 no warnings 'uninitialized';
  35         760  
  35         3516  
809 400 100       1132 return '' unless(length $self->label);
810 392         1074 return $self->label_object(@_)->html_tag;
811             }
812              
813             sub xhtml_label
814             {
815 25     25 1 63 my($self) = shift;
816 35     35   268 no warnings 'uninitialized';
  35         95  
  35         22106  
817 25 100       93 return '' unless(length $self->label);
818 11         51 return $self->label_object(@_)->xhtml_tag;
819             }
820              
821             sub validate
822             {
823 214     214 1 379 my($self) = shift;
824              
825 214         763 $self->error(undef);
826              
827 214         657 my $value = $self->internal_value;
828              
829 214 100 66     759 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     287 unless($self->is_empty && $self->empty_is_ok)
834             {
835 21         552 my $label = $self->error_label;
836              
837 21 100       62 if(defined $label)
838             {
839             #$self->add_error_id(FIELD_REQUIRED, $label);
840             #$self->add_error_id(FIELD_REQUIRED, [ $label ]);
841 7         38 $self->add_error_id(FIELD_REQUIRED, { label => $label });
842             }
843             else
844             {
845 14         111 $self->add_error_id(FIELD_REQUIRED);
846             }
847              
848 21         124 return 0;
849             }
850             }
851              
852 193 100       1868 return $self->validate_with_validator($value) if($self->validator);
853              
854 191         519 return 1;
855             }
856              
857             sub validate_with_validator
858             {
859 2     2 0 7 my($self, $value) = @_;
860              
861 2 50       5 my $code = $self->validator or return 1;
862              
863 2         5 local $_ = $value;
864             #$Debug && warn "running $code->($self)\n";
865 2         7 my $ok = $code->($self);
866              
867 2 50 66     25 if(!$ok && !$self->has_errors)
868             {
869 1         6 my $label = $self->label;
870              
871 1 50       4 if(defined $label)
872             {
873 0         0 $self->add_error_id(FIELD_INVALID, { label => $label })
874             }
875             else
876             {
877 1         8 $self->add_error_id(FIELD_INVALID);
878             }
879             }
880              
881 2         8 return $ok;
882             }
883              
884             sub validator
885             {
886 214     214 1 388 my($self) = shift;
887              
888 214 100       479 if(@_)
889             {
890 2         11 my $code = shift;
891              
892 2 50       15 if(ref $code eq 'CODE')
893             {
894 2         6 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 447 my($self, %args) = @_;
910              
911 90         246 my $error_id = $args{'error_id'};
912 90   66     259 my $msg_class = $args{'msg_class'} || $self->localizer->message_class;
913 90   100     448 my $args = $args{'args'} || [];
914              
915 35     35   441 no warnings 'uninitialized';
  35         92  
  35         28739  
916 90 100       372 if($error_id == FIELD_REQUIRED)
    100          
917             {
918 25         141 my $msg = $msg_class->new(args => $args);
919              
920 25 100 66     309 if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
      33        
      66        
921             {
922 6         25 $msg->id(FIELD_REQUIRED_LABELLED);
923             }
924             else
925             {
926 19         91 $msg->id(FIELD_REQUIRED_GENERIC);
927             }
928              
929 25         138 return $msg;
930             }
931             elsif($error_id == FIELD_INVALID)
932             {
933 1         7 my $msg = $msg_class->new(args => $args);
934              
935 1 50 33     15 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         6 $msg->id(FIELD_INVALID_GENERIC);
942             }
943              
944 1         5 return $msg;
945             }
946              
947 64         241 return undef;
948             }
949              
950 1     1 0 22 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 5466     5466 1 13888 my($invocant) = shift;
958              
959             # Called as object method
960 5466 100       11371 if(my $class = ref $invocant)
961             {
962 5322 50       10589 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 5322         8473 my $localizer = $invocant->{'localizer'};
977              
978 5322 50       9954 unless($localizer)
979             {
980 5322 100       10906 if(my $parent_field = $invocant->parent_field)
    100          
981             {
982 21 50       74 if(my $localizer = $parent_field->localizer)
983             {
984 21 50       1045 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
985             }
986             }
987             elsif(my $parent_form = $invocant->parent_form)
988             {
989 183 50       521 if(my $localizer = $parent_form->localizer)
990             {
991 183 50       8005 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
992             }
993             }
994 5118         13994 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       488 if(@_)
1007             {
1008 1         8 return $invocant->default_localizer(shift);
1009             }
1010              
1011 143         904 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 4085     4085 1 6709 my($invocant) = shift;
1020              
1021             # Called as object method
1022 4085 50       7952 if(my $class = ref $invocant)
1023             {
1024 4085 100       7746 if(@_)
1025             {
1026 20         73 $invocant->{'locale'} = shift;
1027              
1028 20 50       67 if(ref $invocant->{'locale'} eq 'CODE')
1029             {
1030 0         0 return $invocant->{'locale'}->();
1031             }
1032             else
1033             {
1034 20         50 return $invocant->{'locale'};
1035             }
1036             }
1037              
1038 4065         6268 my $locale = $invocant->{'locale'};
1039              
1040 4065 100       7283 if($locale)
1041             {
1042 48 50       208 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1043             }
1044              
1045 4017 100 66     8530 if(my $parent_field = $invocant->parent_field)
    100          
    100          
1046             {
1047 17 50       59 if(my $locale = $parent_field->locale)
1048             {
1049 17 50       81 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1050             }
1051             }
1052             elsif(my $parent_form = $invocant->parent_form)
1053             {
1054 308 50       731 if(my $locale = $parent_form->locale)
1055             {
1056 308 50       1166 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1057             }
1058             }
1059             elsif($invocant->can('parent_group') && (my $parent_group = $invocant->parent_group))
1060             {
1061 1606 50       3534 if(my $locale = $parent_group->locale)
1062             {
1063 1606 50       6372 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1064             }
1065             }
1066             else
1067             {
1068 2086         4137 my $locale = $invocant->localizer->locale;
1069              
1070 2086 50       95610 if(ref $locale eq 'CODE')
1071             {
1072 0         0 return $locale->();
1073             }
1074              
1075 2086   33     8091 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   311 use utf8; # The __DATA__ section contains UTF-8 text
  35         77  
  35         419  
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.