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   516104 use strict;
  35         118  
  35         1356  
4              
5 35     35   217 use Carp();
  35         86  
  35         613  
6 35     35   182 use Scalar::Util();
  35         74  
  35         591  
7              
8 35     35   12854 use Rose::HTML::Util();
  35         101  
  35         1127  
9              
10 35     35   17550 use Rose::HTML::Label;
  35         158  
  35         282  
11 35     35   259 use Rose::HTML::Object::Errors qw(:field);
  35         75  
  35         347  
12 35     35   250 use Rose::HTML::Object::Messages qw(:field);
  35         74  
  35         242  
13              
14 35     35   336 use base 'Rose::HTML::Object';
  35         85  
  35         5535  
15              
16 35     35   267 use constant HTML_ERROR_SEP => "<br>\n";
  35         70  
  35         3841  
17 35     35   256 use constant XHTML_ERROR_SEP => "<br />\n";
  35         127  
  35         2353  
18              
19 35     35   17923 use Rose::HTML::Form::Constants qw(FF_SEPARATOR);
  35         110  
  35         3944  
20              
21             our $VERSION = '0.617';
22              
23             #our $Debug = 0;
24              
25             use Rose::HTML::Object::MakeMethods::Localization
26             (
27 35         435 localized_message =>
28             [
29             qw(_error_label label description)
30             ],
31 35     35   263 );
  35         84  
32              
33             use Rose::Object::MakeMethods::Generic
34             (
35 35         606 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   4150 );
  35         93  
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 74 sub is_button { 0 }
62              
63             *label_id = \&label_message_id;
64              
65             sub error_label
66             {
67 61     61 1 140 my($self) = shift;
68              
69 61 100       163 if(@_)
70             {
71 2         13 return $self->_error_label(@_);
72             }
73              
74 59         252 my $label = $self->_error_label;
75              
76 35     35   6951 no warnings 'uninitialized';
  35         78  
  35         5473  
77 59 100       284 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    100          
78             }
79              
80             sub error_label_id
81             {
82 128     128 1 999 my($self) = shift;
83              
84 128 50       326 if(@_)
85             {
86 128         546 return $self->_error_label_message_id(@_);
87             }
88              
89 0         0 my $label = $self->_error_label_message_id;
90              
91 35     35   284 no warnings 'uninitialized';
  35         121  
  35         38913  
92 0 0       0 return (defined $label) ? (length $label ? $label : undef) : $self->label;
    0          
93             }
94              
95 302     302 0 4617 sub init_apply_error_class { 1 }
96              
97             sub auto_invalidate_parent
98             {
99 2830     2830 1 4722 my($self) = shift;
100              
101 2830 50       5552 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 2830 100       9286 ($self->{'auto_invalidate_parent'} = 1);
109             }
110              
111             sub invalidate_value
112             {
113 89     89 1 196 $_[0]->{'input_value'} = undef;
114 89         174 $_[0]->{'internal_value'} = undef;
115 89         239 $_[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 5311     5311 1 7823 my($self) = shift;
126              
127 5311 100       9810 if(@_)
128             {
129 250 50       559 if(ref $_[0])
130             {
131 250         755 Scalar::Util::weaken($self->{'parent_group'} = shift);
132 250         740 return $self->{'parent_group'};
133             }
134             else
135             {
136 0         0 return $self->{'parent_group'} = shift;
137             }
138             }
139              
140 5061         15080 return $self->{'parent_group'};
141             }
142              
143             sub parent_field
144             {
145 32099     32099 1 49253 my($self) = shift;
146              
147 32099 100       62990 if(@_)
148             {
149 317 50       851 if(ref $_[0])
150             {
151 317         1167 Scalar::Util::weaken($self->{'parent_field'} = shift);
152 317         999 return $self->{'parent_field'};
153             }
154             else
155             {
156 0         0 return $self->{'parent_field'} = shift;
157             }
158             }
159              
160 31782         103417 return $self->{'parent_field'};
161             }
162              
163             sub parent_form
164             {
165 39369     39369 1 60317 my($self) = shift;
166              
167 39369 100       75171 if(@_)
168             {
169 330 50       913 if(ref $_[0])
170             {
171 330         1122 Scalar::Util::weaken($self->{'parent_form'} = shift);
172 330         922 return $self->{'parent_form'};
173             }
174             else
175             {
176 0         0 return $self->{'parent_form'} = shift;
177             }
178             }
179              
180 39039         113865 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 11723     11723 0 18478 my($self) = shift;
200 11723         24472 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  35169         88333  
201             $self->field_context_name,
202             $self->local_name);
203             }
204              
205             sub fq_moniker
206             {
207 1798     1798 0 3077 my($self) = shift;
208              
209 1798         4332 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
  5394         23821  
210             $self->field_context_name,
211             $self->local_moniker);
212             }
213              
214 66     66 0 838 sub init_local_moniker { shift->local_name }
215              
216             sub form_context_name
217             {
218 14851     14851 0 23281 my($self) = shift;
219 14851 100       29468 my $parent_form = $self->parent_form or return undef;
220 6047         18975 return $parent_form->fq_form_name;
221             }
222              
223             sub field_context_name
224             {
225 14851     14851 0 24841 my($self) = shift;
226 14851 100       31620 my $parent_field = $self->parent_field or return undef;
227 5881 50       13648 my $name = $parent_field->fq_name or return undef;
228 5881         16694 return $name;
229             }
230              
231 11     11 0 32 sub is_flat_group { 0 }
232              
233 32     32 0 418 sub init_html_prefix { '' }
234 32     32 0 594 sub init_html_suffix { '' }
235              
236 5     5 0 1798 sub init_html_error_separator { HTML_ERROR_SEP }
237 4     4 0 84 sub init_xhtml_error_separator { XHTML_ERROR_SEP }
238              
239             sub value
240             {
241 167     167 1 1203 my($self) = shift;
242              
243 167 50       482 if(@_)
244             {
245 167         545 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 3226     3226 0 5429 my($self) = shift;
256              
257 3226         10154 $self->html_attr('name', undef);
258 3226 50 66     8817 $self->name if($self->parent_field || $self->parent_form);
259             #$self->name($self->fq_name);
260             }
261              
262             sub local_name
263             {
264 14441     14441 1 23223 my($self) = shift;
265              
266 14441 100       30583 if(@_)
267             {
268 1546         2977 my $name = shift;
269              
270 35     35   305 no warnings 'uninitialized';
  35         117  
  35         12454  
271 1546 50 66     5516 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         3435 my $old_name = $self->{'local_name'};
277 1546         3276 $self->{'local_name'} = $name;
278              
279 1546 100 100     6105 if(defined $old_name && $name ne $old_name)
280             {
281 3 100       12 if(my $parent_form = $self->parent_form)
282             {
283 1         16 $parent_form->delete_field($old_name);
284 1         19 $parent_form->add_field($name => $self);
285             }
286              
287 3 50       10 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         3355 return $name;
295             }
296              
297 12895         24923 my $name = $self->{'local_name'};
298 12895 100       36738 return $name if(defined $name);
299 473         1912 return $self->{'local_name'} = $self->{'local_moniker'};
300             }
301              
302             sub name
303             {
304 7905     7905 1 18403 my($self) = shift;
305              
306 7905 100       18366 if(@_)
307             {
308 558         1947 $self->local_name(shift);
309 558         2125 return $self->html_attr('name', $self->fq_name);
310             }
311              
312 7347         18869 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   260 no warnings 'uninitialized';
  35         86  
  35         43300  
317 7347 100       18597 unless(length $name)
318             {
319 2586         7083 return $self->html_attr('name', $self->fq_name);
320             }
321              
322 4761         18644 return $name;
323             }
324              
325             sub moniker
326             {
327 759     759 0 1446 my($self) = shift;
328              
329 759 100       1651 if(@_)
330             {
331 258         1246 return $self->fq_moniker($self->{'moniker'} = shift);
332             }
333             else
334             {
335 501 100       1850 return $self->{'moniker'} if(defined $self->{'moniker'});
336 57         192 return $self->{'moniker'} = $self->fq_moniker;
337             }
338             }
339              
340             sub default_value
341             {
342 4347     4347 1 8020 my($self) = shift;
343              
344 4347 100       8919 if(@_)
345             {
346 549         1168 $self->{'internal_value'} = undef;
347 549         1014 $self->{'output_value'} = undef;
348 549         1985 return $self->{'default_value'} = shift;
349             }
350              
351 3798         10259 return $self->{'default_value'};
352             }
353              
354 30     30 1 435 sub default { shift->default_value(@_) }
355              
356 1439     1439 1 2805 sub inflate_value { $_[1] }
357 254     254 1 560 sub deflate_value { $_[1] }
358              
359             sub input_value
360             {
361 5273     5273 1 38028 my($self) = shift;
362              
363 5273 100       11299 if(@_)
364             {
365 2553         5417 $self->{'is_cleared'} = 0;
366 2553         4826 $self->{'internal_value'} = undef;
367 2553         5293 $self->{'output_value'} = undef;
368 2553         5632 $self->{'errors'} = undef;
369 2553         5346 $self->{'input_value'} = shift;
370              
371 2553 100       6793 if(my $parent = $self->parent_field)
372             {
373 1003 100 100     4070 $parent->is_cleared(0) if(!$parent->{'in_init'} && $parent->_is_full);
374              
375 1003 100       3341 if($self->auto_invalidate_parent)
376             {
377 54         308 $parent->invalidate_value;
378             }
379             }
380              
381 2553         9005 return $self->{'input_value'};
382             }
383              
384 2720 100 100     6467 return undef if($self->is_cleared || $self->has_partial_value);
385              
386             my $value =
387 2651 100       32158 (defined $self->{'input_value'}) ? $self->{'input_value'} :
388             $self->default_value;
389              
390 2651 50 33     6659 if(wantarray && ref $value eq 'ARRAY')
391             {
392 0         0 return @$value;
393             }
394              
395 2651         6086 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 2094     2094   6382 local $_[0]->{'auto_invalidate_parent'} = 0;
403 2094         5768 shift->input_value(@_);
404             }
405              
406             sub _is_full
407             {
408 1106     1106   1938 my($self) = shift;
409              
410 1106 100       3795 if($self->is_full(@_))
411             {
412 204         739 $self->has_partial_value(0);
413 204         1733 return 1;
414             }
415              
416 902         4343 return 0;
417             }
418              
419             sub input_value_filtered
420             {
421 78     78 1 235 my($self) = shift;
422              
423 78         245 my $value = $self->input_value;
424              
425 78         313 $value = $self->input_prefilter($value);
426              
427 78 100       223 if(my $input_filter = $self->input_filter)
428             {
429 4         12 local $_ = $value;
430 4         19 $value = $input_filter->($self, $value);
431             }
432              
433 78         455 return $value;
434             }
435              
436             sub internal_value
437             {
438 6168     6168 1 13766 my($self) = shift;
439              
440 6168 50       13106 Carp::croak "Cannot set the internal value. Use input_value() instead." if(@_);
441              
442 6168 100 100     15251 return undef if($self->is_cleared || $self->has_partial_value);
443              
444 3149 100       37547 if(defined $self->{'internal_value'})
445             {
446 1278 100 100     3940 if(wantarray && ref $self->{'internal_value'} eq 'ARRAY')
447             {
448 2         5 return @{$self->{'internal_value'}};
  2         10  
449             }
450              
451 1276         4902 return $self->{'internal_value'};
452             }
453              
454 1871         4988 my $value = $self->input_value;
455              
456 1871         3538 my($using_default, $final_value);
457              
458 1871 100       4130 unless(defined $value)
459             {
460 1188         2648 $value = $self->default_value;
461 1188         2265 $using_default++;
462             }
463              
464 1871         5651 $value = $self->input_prefilter($value);
465              
466 1871 100       4829 if(my $input_filter = $self->input_filter)
467             {
468 29         72 local $_ = $value;
469 29         147 $final_value = $input_filter->($self, $value);
470             }
471 1842         3367 else { $final_value = $value }
472              
473 1871         5869 $final_value = $self->inflate_value($final_value);
474              
475 1871 100       4662 $self->{'internal_value'} = $final_value unless($using_default);
476              
477 1871 100 100     6124 if(wantarray && ref $final_value eq 'ARRAY')
478             {
479 2         15 return @$final_value;
480             }
481              
482 1869         6623 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 13987 my($self) = shift;
490              
491 554 50       1551 Carp::croak "Cannot set the output value. Use input_value() instead." if(@_);
492              
493 554 100       2112 return undef if($self->is_cleared);
494              
495 498 100       4630 return $self->{'output_value'} if(defined $self->{'output_value'});
496              
497 322         988 my $value = $self->deflate_value(scalar $self->internal_value);
498              
499 322 100       7700 if(my $output_filter = $self->output_filter)
500             {
501 22         57 local $_ = $value;
502 22         88 $self->{'output_value'} = $output_filter->($self, $value);
503             }
504 300         688 else { $self->{'output_value'} = $value }
505              
506 322 50 66     1639 if(wantarray && ref $self->{'output_value'} eq 'ARRAY')
507             {
508 0         0 return @{$self->{'output_value'}};
  0         0  
509             }
510              
511 322         1673 return $self->{'output_value'};
512             }
513              
514             sub is_empty
515             {
516 2935     2935 1 5206 my($self) = shift;
517              
518 2935         6523 my $value = $self->internal_value;
519              
520 35     35   376 no warnings;
  35         90  
  35         14736  
521 2935 100 66     21218 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 1949     1949 1 3994 my($self, $value) = @_;
529              
530 1949 100       5090 return undef unless(defined $value);
531              
532 737 100       1804 unless(ref $value)
533             {
534 700         1523 for($value)
535             {
536 35     35   274 no warnings;
  35         111  
  35         22167  
537              
538 700 100       2269 if($self->trim_spaces)
539             {
540 698         6918 s/^\s+//;
541 698         2715 s/\s+$//;
542             }
543             }
544             }
545              
546 737         2029 return $value;
547             }
548              
549             sub input_filter
550             {
551 1960     1960 1 4471 my($self) = shift;
552              
553 1960 100       4236 if(@_)
554             {
555 11         86 $self->{'internal_value'} = undef;
556 11         44 $self->{'output_value'} = undef;
557 11         302 return $self->{'input_filter'} = shift;
558             }
559              
560 1949         5297 return $self->{'input_filter'};
561             }
562              
563             sub output_filter
564             {
565 337     337 1 4718 my($self) = shift;
566              
567 337 100       881 if(@_)
568             {
569 14         41 $self->{'output_value'} = undef;
570 14         75 return $self->{'output_filter'} = shift;
571             }
572              
573 323         954 return $self->{'output_filter'};
574             }
575              
576             sub filter
577             {
578 1     1 1 4 my($self) = shift;
579              
580 1 50       6 if(@_)
581             {
582 1         3 my $filter = shift;
583 1         7 $self->{'input_filter'} = $filter;
584 1         5 $self->{'output_filter'} = $filter;
585             }
586              
587 1         2 my $input_filter = $self->{'input_filter'};
588              
589 1 50 33     8 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 1504     1504 1 3878 my($self) = shift;
600              
601 1504         4656 $self->_set_input_value(undef);
602 1504         5641 $self->error(undef);
603 1504         4771 $self->has_partial_value(0);
604 1504         7676 $self->is_cleared(1);
605             }
606              
607             sub reset
608             {
609 66     66 1 219 my($self) = shift;
610              
611 66         264 $self->_set_input_value(undef);
612 66         302 $self->error(undef);
613 66         209 $self->has_partial_value(0);
614 66         408 $self->is_cleared(0);
615 66         318 return 1;
616             }
617              
618             sub hidden_fields
619             {
620 115     115 1 5344 my($self) = shift;
621              
622 115         704 my $hidden_field_class = ref($self)->object_type_class_loaded('hidden');
623              
624 35     35   312 no strict 'refs';
  35         124  
  35         41397  
625 115 50       217 unless(@{$hidden_field_class . '::ISA'})
  115         840  
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         418 $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 363 sub hidden_field { shift->hidden_fields(@_) }
649              
650             sub html_hidden_fields
651             {
652 12     12 1 36 my($self) = shift;
653              
654 12         29 my @html;
655              
656 12         82 foreach my $field ($self->hidden_fields)
657             {
658 18         114 push(@html, $field->html_field);
659             }
660              
661 12 50       2357 return (wantarray) ? @html : join("\n", @html);
662             }
663              
664 8     8 1 63 sub html_hidden_field { shift->html_hidden_fields(@_) }
665              
666             sub xhtml_hidden_fields
667             {
668 7     7 1 53 my($self) = shift;
669              
670 7         18 my @xhtml;
671              
672 7         38 foreach my $field ($self->hidden_fields)
673             {
674 9         92 push(@xhtml, $field->xhtml_field);
675             }
676              
677 7 50       107 return (wantarray) ? @xhtml : join("\n", @xhtml);
678             }
679              
680 3     3 1 16 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 3733 my($self) = shift;
692              
693 943 100 66     3174 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
694             {
695 2         25 $self->add_class('error');
696              
697 2         16 my $html = $self->Rose::HTML::Object::html_tag(@_);
698              
699 2         28 $self->delete_class('error');
700              
701 2         14 return $html;
702             }
703             else
704             {
705 941         3056 $self->SUPER::html_tag(@_);
706             }
707             }
708              
709             sub xhtml_tag
710             {
711 453     453 1 927 my($self) = shift;
712              
713 453 100 66     1468 if($self->html_element && $self->apply_error_class && defined $self->error)
      100        
714             {
715 2         17 $self->add_class('error');
716              
717 2         14 my $html = $self->Rose::HTML::Object::xhtml_tag(@_);
718              
719 2         21 $self->delete_class('error');
720              
721 2         11 return $html;
722             }
723             else
724             {
725 451         1792 $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 250 my($self) = shift;
735              
736 50         127 my($field, $error);
737              
738 50         244 $field = $self->html_field;
739 50         429 $error = $self->html_error;
740              
741 50 100       259 if($error)
742             {
743 5         50 return $field . $self->html_error_separator . $error;
744             }
745              
746 45         359 return $field;
747             }
748              
749             sub xhtml
750             {
751 32     32 1 437 my($self) = shift;
752              
753 32         87 my($field, $error);
754              
755 32         175 $field = $self->xhtml_field;
756 32         260 $error = $self->xhtml_error;
757              
758 32 100       119 if($error)
759             {
760 4         54 return $field . $self->xhtml_error_separator . $error;
761             }
762              
763 28         247 return $field;
764             }
765              
766             # sub label
767             # {
768             #
769             # }
770              
771             sub label_object
772             {
773 406     406 1 623 my($self) = shift;
774 406   66     1228 my $label = $self->{'label_object'} ||= ref($self)->object_type_class_loaded('label')->new();
775              
776 406 100       1275 $label->contents($self->escape_html ? __escape_html($self->label) :
777             $self->label);
778              
779 406 100       1703 if($self->html_attr_exists('id'))
780             {
781 26         100 $label->for($self->html_attr('id'));
782             }
783              
784             $self->required ?
785 406 100       1180 $label->add_class('required') :
786             $label->delete_class('required');
787              
788 406 100       1799 defined $self->error ?
789             $label->add_class('error') :
790             $label->delete_class('error');
791              
792 406 50       1377 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         1353 return $label;
803             }
804              
805             sub html_label
806             {
807 400     400 1 672 my($self) = shift;
808 35     35   296 no warnings 'uninitialized';
  35         101  
  35         4266  
809 400 100       1195 return '' unless(length $self->label);
810 392         1277 return $self->label_object(@_)->html_tag;
811             }
812              
813             sub xhtml_label
814             {
815 25     25 1 77 my($self) = shift;
816 35     35   272 no warnings 'uninitialized';
  35         133  
  35         22043  
817 25 100       130 return '' unless(length $self->label);
818 11         60 return $self->label_object(@_)->xhtml_tag;
819             }
820              
821             sub validate
822             {
823 214     214 1 465 my($self) = shift;
824              
825 214         985 $self->error(undef);
826              
827 214         5283 my $value = $self->internal_value;
828              
829 214 100 66     857 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     344 unless($self->is_empty && $self->empty_is_ok)
834             {
835 21         654 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         60 $self->add_error_id(FIELD_REQUIRED, { label => $label });
842             }
843             else
844             {
845 14         87 $self->add_error_id(FIELD_REQUIRED);
846             }
847              
848 21         109 return 0;
849             }
850             }
851              
852 193 100       1983 return $self->validate_with_validator($value) if($self->validator);
853              
854 191         595 return 1;
855             }
856              
857             sub validate_with_validator
858             {
859 2     2 0 11 my($self, $value) = @_;
860              
861 2 50       8 my $code = $self->validator or return 1;
862              
863 2         7 local $_ = $value;
864             #$Debug && warn "running $code->($self)\n";
865 2         14 my $ok = $code->($self);
866              
867 2 50 66     48 if(!$ok && !$self->has_errors)
868             {
869 1         26 my $label = $self->label;
870              
871 1 50       5 if(defined $label)
872             {
873 0         0 $self->add_error_id(FIELD_INVALID, { label => $label })
874             }
875             else
876             {
877 1         26 $self->add_error_id(FIELD_INVALID);
878             }
879             }
880              
881 2         9 return $ok;
882             }
883              
884             sub validator
885             {
886 214     214 1 442 my($self) = shift;
887              
888 214 100       526 if(@_)
889             {
890 2         5 my $code = shift;
891              
892 2 50       12 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         651 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 495 my($self, %args) = @_;
910              
911 90         235 my $error_id = $args{'error_id'};
912 90   66     399 my $msg_class = $args{'msg_class'} || $self->localizer->message_class;
913 90   100     449 my $args = $args{'args'} || [];
914              
915 35     35   279 no warnings 'uninitialized';
  35         75  
  35         34159  
916 90 100       413 if($error_id == FIELD_REQUIRED)
    100          
917             {
918 25         199 my $msg = $msg_class->new(args => $args);
919              
920 25 100 66     296 if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
      33        
      66        
921             {
922 6         35 $msg->id(FIELD_REQUIRED_LABELLED);
923             }
924             else
925             {
926 19         86 $msg->id(FIELD_REQUIRED_GENERIC);
927             }
928              
929 25         126 return $msg;
930             }
931             elsif($error_id == FIELD_INVALID)
932             {
933 1         11 my $msg = $msg_class->new(args => $args);
934              
935 1 50 33     42 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         10 $msg->id(FIELD_INVALID_GENERIC);
942             }
943              
944 1         6 return $msg;
945             }
946              
947 64         266 return undef;
948             }
949              
950 1     1 0 46 sub localize_label { shift->label_message_id(FIELD_LABEL) }
951 1     1 0 14 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 5483     5483 1 13111 my($invocant) = shift;
958              
959             # Called as object method
960 5483 100       12135 if(my $class = ref $invocant)
961             {
962 5339 50       10993 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 5339         8813 my $localizer = $invocant->{'localizer'};
977              
978 5339 50       10127 unless($localizer)
979             {
980 5339 100       11707 if(my $parent_field = $invocant->parent_field)
    100          
981             {
982 21 50       92 if(my $localizer = $parent_field->localizer)
983             {
984 21 50       1456 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
985             }
986             }
987             elsif(my $parent_form = $invocant->parent_form)
988             {
989 183 50       546 if(my $localizer = $parent_form->localizer)
990             {
991 183 50       8590 return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
992             }
993             }
994 5135         15470 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       592 if(@_)
1007             {
1008 1         12 return $invocant->default_localizer(shift);
1009             }
1010              
1011 143         1415 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 4106     4106 1 6046 my($invocant) = shift;
1020              
1021             # Called as object method
1022 4106 50       7352 if(my $class = ref $invocant)
1023             {
1024 4106 100       7714 if(@_)
1025             {
1026 20         66 $invocant->{'locale'} = shift;
1027              
1028 20 50       75 if(ref $invocant->{'locale'} eq 'CODE')
1029             {
1030 0         0 return $invocant->{'locale'}->();
1031             }
1032             else
1033             {
1034 20         67 return $invocant->{'locale'};
1035             }
1036             }
1037              
1038 4086         6327 my $locale = $invocant->{'locale'};
1039              
1040 4086 100       7193 if($locale)
1041             {
1042 48 50       270 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1043             }
1044              
1045 4038 100 66     8842 if(my $parent_field = $invocant->parent_field)
    100          
    100          
1046             {
1047 17 50       120 if(my $locale = $parent_field->locale)
1048             {
1049 17 50       116 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1050             }
1051             }
1052             elsif(my $parent_form = $invocant->parent_form)
1053             {
1054 315 50       823 if(my $locale = $parent_form->locale)
1055             {
1056 315 50       1428 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1057             }
1058             }
1059             elsif($invocant->can('parent_group') && (my $parent_group = $invocant->parent_group))
1060             {
1061 1613 50       3755 if(my $locale = $parent_group->locale)
1062             {
1063 1613 50       5909 return (ref $locale eq 'CODE') ? $locale->() : $locale;
1064             }
1065             }
1066             else
1067             {
1068 2093         4089 my $locale = $invocant->localizer->locale;
1069              
1070 2093 50       93882 if(ref $locale eq 'CODE')
1071             {
1072 0         0 return $locale->();
1073             }
1074              
1075 2093   33     8999 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   323 use utf8; # The __DATA__ section contains UTF-8 text
  35         99  
  35         382  
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.