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              
2             use strict;
3 35     35   103571  
  35         74  
  35         967  
4             use Carp();
5 35     35   157 use Scalar::Util();
  35         57  
  35         403  
6 35     35   139  
  35         55  
  35         422  
7             use Rose::HTML::Util();
8 35     35   9246  
  35         80  
  35         716  
9             use Rose::HTML::Label;
10 35     35   12042 use Rose::HTML::Object::Errors qw(:field);
  35         103  
  35         317  
11 35     35   198 use Rose::HTML::Object::Messages qw(:field);
  35         62  
  35         263  
12 35     35   193  
  35         76  
  35         175  
13             use base 'Rose::HTML::Object';
14 35     35   223  
  35         81  
  35         3627  
15             use constant HTML_ERROR_SEP => "<br>\n";
16 35     35   360 use constant XHTML_ERROR_SEP => "<br />\n";
  35         69  
  35         2753  
17 35     35   216  
  35         60  
  35         1733  
18             use Rose::HTML::Form::Constants qw(FF_SEPARATOR);
19 35     35   11766  
  35         77  
  35         2653  
20             our $VERSION = '0.617';
21              
22             #our $Debug = 0;
23              
24             use Rose::HTML::Object::MakeMethods::Localization
25             (
26             localized_message =>
27 35         324 [
28             qw(_error_label label description)
29             ],
30             );
31 35     35   217  
  35         81  
32             use Rose::Object::MakeMethods::Generic
33             (
34             scalar => [ qw(rank type) ],
35 35         567  
36             boolean => [ qw(required is_cleared has_partial_value) ],
37             boolean =>
38             [
39             trim_spaces => { default => 1 },
40             empty_is_ok => { default => 0 },
41             ],
42              
43             'scalar --get_set_init' =>
44             [
45             qw(html_prefix html_suffix html_error_separator xhtml_error_separator
46             local_moniker apply_error_class)
47             ],
48             );
49 35     35   2934  
  35         74  
50             __PACKAGE__->add_valid_html_attrs(qw(
51             name
52             value
53             onblur
54             onfocus
55             accesskey
56             tabindex
57             autofocus
58             ));
59              
60              
61 20     20 0 48 *label_id = \&label_message_id;
62              
63             {
64             my($self) = shift;
65              
66             if(@_)
67 61     61 1 108 {
68             return $self->_error_label(@_);
69 61 100       130 }
70              
71 2         9 my $label = $self->_error_label;
72              
73             no warnings 'uninitialized';
74 59         172 return (defined $label) ? (length $label ? $label : undef) : $self->label;
75             }
76 35     35   4477  
  35         71  
  35         3992  
77 59 100       221 {
    100          
78             my($self) = shift;
79              
80             if(@_)
81             {
82 128     128 1 653 return $self->_error_label_message_id(@_);
83             }
84 128 50       268  
85             my $label = $self->_error_label_message_id;
86 128         342  
87             no warnings 'uninitialized';
88             return (defined $label) ? (length $label ? $label : undef) : $self->label;
89 0         0 }
90              
91 35     35   319  
  35         77  
  35         30183  
92 0 0       0 {
    0          
93             my($self) = shift;
94              
95 300     300 0 2841 if(@_)
96             {
97             return $self->{'auto_invalidate_parent'} = $_[0] ? 1 : 0;
98             }
99 3182     3182 1 4246  
100             return defined($self->{'auto_invalidate_parent'}) ?
101 3182 50       4850 $self->{'auto_invalidate_parent'} :
102             ($self->{'auto_invalidate_parent'} = 1);
103 0 0       0 }
104              
105             {
106             $_[0]->{'input_value'} = undef;
107             $_[0]->{'internal_value'} = undef;
108 3182 100       7841 $_[0]->{'output_value'} = undef;
109             }
110              
111             {
112             $_[0]->{'output_value'} = undef;
113 89     89 1 144 }
114 89         162  
115 89         151 {
116             my($self) = shift;
117              
118             if(@_)
119             {
120 1     1 1 4 if(ref $_[0])
121             {
122             Scalar::Util::weaken($self->{'parent_group'} = shift);
123             return $self->{'parent_group'};
124             }
125 5381     5381 1 7226 else
126             {
127 5381 100       8347 return $self->{'parent_group'} = shift;
128             }
129 249 50       493 }
130              
131 249         862 return $self->{'parent_group'};
132 249         536 }
133              
134             {
135             my($self) = shift;
136 0         0  
137             if(@_)
138             {
139             if(ref $_[0])
140 5132         12730 {
141             Scalar::Util::weaken($self->{'parent_field'} = shift);
142             return $self->{'parent_field'};
143             }
144             else
145 33406     33406 1 39874 {
146             return $self->{'parent_field'} = shift;
147 33406 100       50431 }
148             }
149 317 50       591  
150             return $self->{'parent_field'};
151 317         1002 }
152 317         655  
153             {
154             my($self) = shift;
155              
156 0         0 if(@_)
157             {
158             if(ref $_[0])
159             {
160 33089         74059 Scalar::Util::weaken($self->{'parent_form'} = shift);
161             return $self->{'parent_form'};
162             }
163             else
164             {
165 40376     40376 1 47740 return $self->{'parent_form'} = shift;
166             }
167 40376 100       59753 }
168              
169 329 50       644 return $self->{'parent_form'};
170             }
171 329         1252  
172 329         641 {
173             my($self) = shift;
174              
175             my $parent = $self->parent_field || $self->parent_form;
176 0         0  
177             return 0 unless($parent);
178              
179             my $depth = 1;
180 40047         82670 $depth++ while($parent = $parent->parent_field || $parent->parent_form);
181              
182             return $depth;
183             }
184              
185 0     0 0 0 {
186             my($self) = shift;
187 0   0     0 return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
188             $self->field_context_name,
189 0 0       0 $self->local_name);
190             }
191 0         0  
192 0   0     0 {
193             my($self) = shift;
194 0         0  
195             return join(FF_SEPARATOR, grep { defined } $self->form_context_name,
196             $self->field_context_name,
197             $self->local_moniker);
198             }
199 12370     12370 0 14748  
200 12370         17151  
  37110         68289  
201             {
202             my($self) = shift;
203             my $parent_form = $self->parent_form or return undef;
204             return $parent_form->fq_form_name;
205             }
206              
207 1780     1780 0 2366 {
208             my($self) = shift;
209 1780         2608 my $parent_field = $self->parent_field or return undef;
  5340         17654  
210             my $name = $parent_field->fq_name or return undef;
211             return $name;
212             }
213              
214 74     74 0 545  
215              
216              
217             {
218 15591     15591 0 18185 my($self) = shift;
219 15591 100       20379  
220 6469         12891 if(@_)
221             {
222             return $self->input_value($self->html_attr('value', shift));
223             }
224             else
225 15591     15591 0 19154 {
226 15591 100       21778 return $self->html_attr('value');
227 6201 50       9478 }
228 6201         12147 }
229              
230             {
231 11     11 0 24 my($self) = shift;
232              
233 32     32 0 311 $self->html_attr('name', undef);
234 32     32 0 425 $self->name if($self->parent_field || $self->parent_form);
235             #$self->name($self->fq_name);
236 5     5 0 77 }
237 4     4 0 78  
238             {
239             my($self) = shift;
240              
241 167     167 1 740 if(@_)
242             {
243 167 50       405 my $name = shift;
244              
245 167         382 no warnings 'uninitialized';
246             if(index($name, FF_SEPARATOR) >= 0 && !$self->isa('Rose::HTML::Form::Field::Hidden'))
247             {
248             Carp::croak "Invalid local field name: $name";
249 0         0 }
250              
251             my $old_name = $self->{'local_name'};
252             $self->{'local_name'} = $name;
253              
254             if(defined $old_name && $name ne $old_name)
255 3369     3369 0 4601 {
256             if(my $parent_form = $self->parent_form)
257 3369         7533 {
258 3369 50 66     5731 $parent_form->delete_field($old_name);
259             $parent_form->add_field($name => $self);
260             }
261              
262             if(my $parent_field = $self->parent_field)
263             {
264 15131     15131 1 20419 $parent_field->delete_field($old_name);
265             $parent_field->add_field($name => $self);
266 15131 100       22282 }
267             }
268 1550         2003  
269             return $name;
270 35     35   263 }
  35         132  
  35         7853  
271 1550 50 66     3931  
272             my $name = $self->{'local_name'};
273 0         0 return $name if(defined $name);
274             return $self->{'local_name'} = $self->{'local_moniker'};
275             }
276 1550         2702  
277 1550         2322 {
278             my($self) = shift;
279 1550 100 100     4136  
280             if(@_)
281 3 100       9 {
282             $self->local_name(shift);
283 1         10 return $self->html_attr('name', $self->fq_name);
284 1         13 }
285              
286             my $name = $self->html_attr('name');
287 3 50       8  
288             # The name HTML attr will be an empty string if it's a required attr,
289 0         0 # so use length() and not defined()
290 0         0 no warnings 'uninitialized';
291             unless(length $name)
292             {
293             return $self->html_attr('name', $self->fq_name);
294 1550         2561 }
295              
296             return $name;
297 13581         18133 }
298 13581 100       26360  
299 482         1084 {
300             my($self) = shift;
301              
302             if(@_)
303             {
304 8310     8310 1 13388 return $self->fq_moniker($self->{'moniker'} = shift);
305             }
306 8310 100       13384 else
307             {
308 557         1509 return $self->{'moniker'} if(defined $self->{'moniker'});
309 557         1376 return $self->{'moniker'} = $self->fq_moniker;
310             }
311             }
312 7753         13888  
313             {
314             my($self) = shift;
315              
316 35     35   295 if(@_)
  35         85  
  35         31267  
317 7753 100       12912 {
318             $self->{'internal_value'} = undef;
319 2758         5293 $self->{'output_value'} = undef;
320             return $self->{'default_value'} = shift;
321             }
322 4995         10909  
323             return $self->{'default_value'};
324             }
325              
326              
327 758     758 0 1152  
328             {
329 758 100       1307 my($self) = shift;
330              
331 257         811 if(@_)
332             {
333             $self->{'is_cleared'} = 0;
334             $self->{'internal_value'} = undef;
335 501 100       1360 $self->{'output_value'} = undef;
336 57         159 $self->{'errors'} = undef;
337             $self->{'input_value'} = shift;
338              
339             if(my $parent = $self->parent_field)
340             {
341             $parent->is_cleared(0) if(!$parent->{'in_init'} && $parent->_is_full);
342 4803     4803 1 6103  
343             if($self->auto_invalidate_parent)
344 4803 100       7188 {
345             $parent->invalidate_value;
346 539         801 }
347 539         765 }
348 539         1272  
349             return $self->{'input_value'};
350             }
351 4264         7407  
352             return undef if($self->is_cleared || $self->has_partial_value);
353              
354 30     30 1 311 my $value =
355             (defined $self->{'input_value'}) ? $self->{'input_value'} :
356 1548     1548 1 2431 $self->default_value;
357 252     252 1 400  
358             if(wantarray && ref $value eq 'ARRAY')
359             {
360             return @$value;
361 5942     5942 1 22264 }
362              
363 5942 100       9339 return $value;
364             }
365 2921         3810  
366 2921         3808 {
367 2921         3709 # XXX: Evil, but I can't bear to add 3 method calls to
368 2921         3798 # XXX: save and then restore this value.
369 2921         3858 local $_[0]->{'auto_invalidate_parent'} = 0;
370             shift->input_value(@_);
371 2921 100       4649 }
372              
373 1211 100 100     2759 {
374             my($self) = shift;
375 1211 100       2404  
376             if($self->is_full(@_))
377 54         164 {
378             $self->has_partial_value(0);
379             return 1;
380             }
381 2921         6098  
382             return 0;
383             }
384 3021 100 100     5254  
385             {
386             my($self) = shift;
387 2952 100       25491  
388             my $value = $self->input_value;
389              
390 2952 50 33     5292 $value = $self->input_prefilter($value);
391              
392 0         0 if(my $input_filter = $self->input_filter)
393             {
394             local $_ = $value;
395 2952         4756 $value = $input_filter->($self, $value);
396             }
397              
398             return $value;
399             }
400              
401             {
402 2462     2462   4084 my($self) = shift;
403 2462         4536  
404             Carp::croak "Cannot set the internal value. Use input_value() instead." if(@_);
405              
406             return undef if($self->is_cleared || $self->has_partial_value);
407              
408 1314     1314   1771 if(defined $self->{'internal_value'})
409             {
410 1314 100       2933 if(wantarray && ref $self->{'internal_value'} eq 'ARRAY')
411             {
412 202         753 return @{$self->{'internal_value'}};
413 202         1207 }
414              
415             return $self->{'internal_value'};
416 1112         3614 }
417              
418             my $value = $self->input_value;
419              
420             my($using_default, $final_value);
421 78     78 1 134  
422             unless(defined $value)
423 78         144 {
424             $value = $self->default_value;
425 78         164 $using_default++;
426             }
427 78 100       149  
428             $value = $self->input_prefilter($value);
429 4         10  
430 4         11 if(my $input_filter = $self->input_filter)
431             {
432             local $_ = $value;
433 78         260 $final_value = $input_filter->($self, $value);
434             }
435             else { $final_value = $value }
436              
437             $final_value = $self->inflate_value($final_value);
438 7300     7300 1 10558  
439             $self->{'internal_value'} = $final_value unless($using_default);
440 7300 50       11134  
441             if(wantarray && ref $final_value eq 'ARRAY')
442 7300 100 100     11673 {
443             return @$final_value;
444 3313 100       29498 }
445              
446 1269 100 100     2614 return $final_value;
447             }
448 2         3  
  2         7  
449              
450             {
451 1267         2970 my($self) = shift;
452              
453             Carp::croak "Cannot set the output value. Use input_value() instead." if(@_);
454 2044         3523  
455             return undef if($self->is_cleared);
456 2044         2767  
457             return $self->{'output_value'} if(defined $self->{'output_value'});
458 2044 100       3471  
459             my $value = $self->deflate_value(scalar $self->internal_value);
460 1360         1921  
461 1360         1730 if(my $output_filter = $self->output_filter)
462             {
463             local $_ = $value;
464 2044         3559 $self->{'output_value'} = $output_filter->($self, $value);
465             }
466 2044 100       3724 else { $self->{'output_value'} = $value }
467              
468 29         47 if(wantarray && ref $self->{'output_value'} eq 'ARRAY')
469 29         62 {
470             return @{$self->{'output_value'}};
471 2015         2675 }
472              
473 2044         4160 return $self->{'output_value'};
474             }
475 2044 100       3532  
476             {
477 2044 100 100     4682 my($self) = shift;
478              
479 2         7 my $value = $self->internal_value;
480              
481             no warnings;
482 2042         4749 return ($value =~ /\S/ || (!$self->trim_spaces && length $value)) ? 0 : 1;
483             }
484              
485 0     0 0 0  
486             {
487             my($self, $value) = @_;
488              
489 548     548 1 9069 return undef unless(defined $value);
490              
491 548 50       1057 unless(ref $value)
492             {
493 548 100       1374 for($value)
494             {
495 492 100       3353 no warnings;
496              
497 320         696 if($self->trim_spaces)
498             {
499 320 100       4537 s/^\s+//;
500             s/\s+$//;
501 22         38 }
502 22         54 }
503             }
504 298         507  
505             return $value;
506 320 50 66     1208 }
507              
508 0         0 {
  0         0  
509             my($self) = shift;
510              
511 320         1253 if(@_)
512             {
513             $self->{'internal_value'} = undef;
514             $self->{'output_value'} = undef;
515             return $self->{'input_filter'} = shift;
516 3605     3605 1 4664 }
517              
518 3605         5035 return $self->{'input_filter'};
519             }
520 35     35   253  
  35         72  
  35         4473  
521 3605 100 66     17974 {
522             my($self) = shift;
523              
524 0     0 1 0 if(@_)
525             {
526             $self->{'output_value'} = undef;
527             return $self->{'output_filter'} = shift;
528 2122     2122 1 3162 }
529              
530 2122 100       3679 return $self->{'output_filter'};
531             }
532 738 100       1420  
533             {
534 701         1095 my($self) = shift;
535              
536 35     35   228 if(@_)
  35         80  
  35         12943  
537             {
538 701 100       1624 my $filter = shift;
539             $self->{'input_filter'} = $filter;
540 699         4530 $self->{'output_filter'} = $filter;
541 699         1726 }
542              
543             my $input_filter = $self->{'input_filter'};
544              
545             if(ref $input_filter && $input_filter eq $self->output_filter)
546 738         1308 {
547             return $input_filter;
548             }
549              
550             return;
551 2133     2133 1 3474 }
552              
553 2133 100       3490 {
554             my($self) = shift;
555 11         23  
556 11         25 $self->_set_input_value(undef);
557 11         32 $self->error(undef);
558             $self->has_partial_value(0);
559             $self->is_cleared(1);
560 2122         3815 }
561              
562             {
563             my($self) = shift;
564              
565 335     335 1 2921 $self->_set_input_value(undef);
566             $self->error(undef);
567 335 100       590 $self->has_partial_value(0);
568             $self->is_cleared(0);
569 14         29 return 1;
570 14         52 }
571              
572             {
573 321         661 my($self) = shift;
574              
575             my $hidden_field_class = ref($self)->object_type_class_loaded('hidden');
576              
577             no strict 'refs';
578 1     1 1 2 unless(@{$hidden_field_class . '::ISA'})
579             {
580 1 50       4 my $error;
581              
582 1         3 TRY:
583 1         4 {
584 1         3 local $@;
585             eval "use $hidden_field_class";
586             $error = $@;
587 1         1 }
588              
589 1 50 33     6 Carp::croak "Could not load hidden field class '$hidden_field_class' - $error"
590             if($error);
591 1         3 }
592              
593             return
594 0         0 $hidden_field_class->new(
595             name => $self->html_attr('name'),
596             id => $self->html_attr('id'),
597             class => $self->html_attr('class'),
598             value => $self->output_value);
599 1808     1808 1 2772 }
600              
601 1808         3298  
602 1808         4270 {
603 1808         3952 my($self) = shift;
604 1808         6140  
605             my @html;
606              
607             foreach my $field ($self->hidden_fields)
608             {
609 66     66 1 116 push(@html, $field->html_field);
610             }
611 66         156  
612 66         224 return (wantarray) ? @html : join("\n", @html);
613 66         230 }
614 66         289  
615 66         244  
616             {
617             my($self) = shift;
618              
619             my @xhtml;
620 115     115 1 983  
621             foreach my $field ($self->hidden_fields)
622 115         350 {
623             push(@xhtml, $field->xhtml_field);
624 35     35   234 }
  35         70  
  35         27703  
625 115 50       180  
  115         478  
626             return (wantarray) ? @xhtml : join("\n", @xhtml);
627 0         0 }
628              
629              
630             {
631 0         0 my($self) = shift;
  0         0  
632 0         0 Carp::croak "Cannot set element for ", ref($self) if(@_);
633 0         0 return $self->html_element;
634             }
635              
636 0 0       0 {
637             my($self) = shift;
638              
639             if($self->html_element && $self->apply_error_class && defined $self->error)
640             {
641 115         267 $self->add_class('error');
642              
643             my $html = $self->Rose::HTML::Object::html_tag(@_);
644              
645             $self->delete_class('error');
646              
647             return $html;
648 86     86 1 219 }
649             else
650             {
651             $self->SUPER::html_tag(@_);
652 12     12 1 29 }
653             }
654 12         18  
655             {
656 12         54 my($self) = shift;
657              
658 18         49 if($self->html_element && $self->apply_error_class && defined $self->error)
659             {
660             $self->add_class('error');
661 12 50       148  
662             my $html = $self->Rose::HTML::Object::xhtml_tag(@_);
663              
664 8     8 1 47 $self->delete_class('error');
665              
666             return $html;
667             }
668 7     7 1 14 else
669             {
670 7         13 $self->SUPER::xhtml_tag(@_);
671             }
672 7         24 }
673              
674 9         30 *html_field = \&html_tag;
675             *xhtml_field = \&xhtml_tag;
676              
677 7 50       65 {
678             my($self) = shift;
679              
680 3     3 1 13 my($field, $error);
681              
682             $field = $self->html_field;
683             $error = $self->html_error;
684 0     0 1 0  
685 0 0       0 if($error)
686 0         0 {
687             return $field . $self->html_error_separator . $error;
688             }
689              
690             return $field;
691 939     939 1 3221 }
692              
693 939 100 66     1976 {
      100        
694             my($self) = shift;
695 1         10  
696             my($field, $error);
697 1         4  
698             $field = $self->xhtml_field;
699 1         9 $error = $self->xhtml_error;
700              
701 1         3 if($error)
702             {
703             return $field . $self->xhtml_error_separator . $error;
704             }
705 938         2230  
706             return $field;
707             }
708              
709             # sub label
710             # {
711 450     450 1 691 #
712             # }
713 450 100 66     985  
      100        
714             {
715 2         10 my($self) = shift;
716             my $label = $self->{'label_object'} ||= ref($self)->object_type_class_loaded('label')->new();
717 2         6  
718             $label->contents($self->escape_html ? __escape_html($self->label) :
719 2         10 $self->label);
720              
721 2         6 if($self->html_attr_exists('id'))
722             {
723             $label->for($self->html_attr('id'));
724             }
725 448         1146  
726             $self->required ?
727             $label->add_class('required') :
728             $label->delete_class('required');
729              
730             defined $self->error ?
731             $label->add_class('error') :
732             $label->delete_class('error');
733              
734 50     50 1 181 if(@_)
735             {
736 50         78 my %args = @_;
737              
738 50         153 while(my($k, $v) = each(%args))
739 50         295 {
740             $label->html_attr($k => $v);
741 50 100       128 }
742             }
743 5         52  
744             return $label;
745             }
746 45         229  
747             {
748             my($self) = shift;
749             no warnings 'uninitialized';
750             return '' unless(length $self->label);
751 32     32 1 403 return $self->label_object(@_)->html_tag;
752             }
753 32         51  
754             {
755 32         92 my($self) = shift;
756 32         146 no warnings 'uninitialized';
757             return '' unless(length $self->label);
758 32 100       80 return $self->label_object(@_)->xhtml_tag;
759             }
760 4         84  
761             {
762             my($self) = shift;
763 28         121  
764             $self->error(undef);
765              
766             my $value = $self->internal_value;
767              
768             if($self->required &&
769             ((!ref $value && (!defined $value || ($self->trim_spaces && $value !~ /\S/))) ||
770             (ref $value eq 'ARRAY' && !@$value)))
771             {
772             unless($self->is_empty && $self->empty_is_ok)
773 406     406 1 581 {
774 406   66     1048 my $label = $self->error_label;
775              
776 406 100       1020 if(defined $label)
777             {
778             #$self->add_error_id(FIELD_REQUIRED, $label);
779 406 100       1074 #$self->add_error_id(FIELD_REQUIRED, [ $label ]);
780             $self->add_error_id(FIELD_REQUIRED, { label => $label });
781 26         83 }
782             else
783             {
784             $self->add_error_id(FIELD_REQUIRED);
785 406 100       903 }
786              
787             return 0;
788 406 100       1621 }
789             }
790              
791             return $self->validate_with_validator($value) if($self->validator);
792 406 50       1050  
793             return 1;
794 0         0 }
795              
796 0         0 {
797             my($self, $value) = @_;
798 0         0  
799             my $code = $self->validator or return 1;
800              
801             local $_ = $value;
802 406         1021 #$Debug && warn "running $code->($self)\n";
803             my $ok = $code->($self);
804              
805             if(!$ok && !$self->has_errors)
806             {
807 400     400 1 686 my $label = $self->label;
808 35     35   249  
  35         648  
  35         2879  
809 400 100       932 if(defined $label)
810 392         852 {
811             $self->add_error_id(FIELD_INVALID, { label => $label })
812             }
813             else
814             {
815 25     25 1 49 $self->add_error_id(FIELD_INVALID);
816 35     35   241 }
  35         78  
  35         14742  
817 25 100       74 }
818 11         39  
819             return $ok;
820             }
821              
822             {
823 214     214 1 304 my($self) = shift;
824              
825 214         665 if(@_)
826             {
827 214         597 my $code = shift;
828              
829 214 100 66     573 if(ref $code eq 'CODE')
      66        
830             {
831             return $self->{'validator'} = $code;
832             }
833 21 50 33     239 else
834             {
835 21         374 Carp::croak ref($self), "::validator() - argument must be a code reference";
836             }
837 21 100       56 }
838              
839             return $self->{'validator'};
840             }
841 7         33  
842             *__escape_html = \&Rose::HTML::Util::escape_html;
843              
844             {
845 14         76 my($self, %args) = @_;
846              
847             my $error_id = $args{'error_id'};
848 21         72 my $msg_class = $args{'msg_class'} || $self->localizer->message_class;
849             my $args = $args{'args'} || [];
850              
851             no warnings 'uninitialized';
852 193 100       1461 if($error_id == FIELD_REQUIRED)
853             {
854 191         641 my $msg = $msg_class->new(args => $args);
855              
856             if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
857             {
858             $msg->id(FIELD_REQUIRED_LABELLED);
859 2     2 0 6 }
860             else
861 2 50       5 {
862             $msg->id(FIELD_REQUIRED_GENERIC);
863 2         6 }
864              
865 2         15 return $msg;
866             }
867 2 50 66     27 elsif($error_id == FIELD_INVALID)
868             {
869 1         5 my $msg = $msg_class->new(args => $args);
870              
871 1 50       4 if((ref $args eq 'HASH' && keys %$args) || (ref $args eq 'ARRAY' && @$args))
872             {
873 0         0 $msg->id(FIELD_INVALID_LABELLED);
874             }
875             else
876             {
877 1         7 $msg->id(FIELD_INVALID_GENERIC);
878             }
879              
880             return $msg;
881 2         8 }
882              
883             return undef;
884             }
885              
886 214     214 1 329  
887             # XXX: Ths sub contains a lame hack to work around an incompatibility with
888 214 100       375 # XXX: Scalar::Defer 0.11 - it manually detects and evaluates code refs.
889             {
890 2         6 my($invocant) = shift;
891              
892 2 50       9 # Called as object method
893             if(my $class = ref $invocant)
894 2         6 {
895             if(@_)
896             {
897             $invocant->{'localizer'} = shift;
898 0         0  
899             if(ref $invocant->{'localizer'} eq 'CODE')
900             {
901             return $invocant->{'localizer'}->();
902 212         457 }
903             else
904             {
905             return $invocant->{'localizer'};
906             }
907             }
908              
909 90     90 1 381 my $localizer = $invocant->{'localizer'};
910              
911 90         213 unless($localizer)
912 90   66     238 {
913 90   100     350 if(my $parent_field = $invocant->parent_field)
914             {
915 35     35   372 if(my $localizer = $parent_field->localizer)
  35         81  
  35         23314  
916 90 100       290 {
    100          
917             return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
918 25         121 }
919             }
920 25 100 66     267 elsif(my $parent_form = $invocant->parent_form)
      33        
      66        
921             {
922 6         25 if(my $localizer = $parent_form->localizer)
923             {
924             return (ref $localizer eq 'CODE') ? $localizer->() : $localizer;
925             }
926 19         85 }
927             else { return $class->default_localizer }
928             }
929 25         76  
930             if(ref $localizer eq 'CODE')
931             {
932             return $localizer->();
933 1         7 }
934              
935 1 50 33     20 return $localizer || $class->default_localizer;
      33        
      33        
936             }
937 0         0 else # Called as class method
938             {
939             if(@_)
940             {
941 1         8 return $invocant->default_localizer(shift);
942             }
943              
944 1         5 return $invocant->default_localizer;
945             }
946             }
947 64         193  
948             # XXX: Ths sub contains a lame hack to work around an incompatibility with
949             # XXX: Scalar::Defer 0.11 - it manually detects and evaluates code refs.
950 1     1 0 19 {
951 1     1 0 10 my($invocant) = shift;
952              
953             # Called as object method
954             if(my $class = ref $invocant)
955             {
956             if(@_)
957 5440     5440 1 11324 {
958             $invocant->{'locale'} = shift;
959              
960 5440 100       9713 if(ref $invocant->{'locale'} eq 'CODE')
961             {
962 5296 50       8755 return $invocant->{'locale'}->();
963             }
964 0         0 else
965             {
966 0 0       0 return $invocant->{'locale'};
967             }
968 0         0 }
969              
970             my $locale = $invocant->{'locale'};
971              
972 0         0 if($locale)
973             {
974             return (ref $locale eq 'CODE') ? $locale->() : $locale;
975             }
976 5296         7226  
977             if(my $parent_field = $invocant->parent_field)
978 5296 50       8328 {
979             if(my $locale = $parent_field->locale)
980 5296 100       9039 {
    100          
981             return (ref $locale eq 'CODE') ? $locale->() : $locale;
982 21 50       46 }
983             }
984 21 50       808 elsif(my $parent_form = $invocant->parent_form)
985             {
986             if(my $locale = $parent_form->locale)
987             {
988             return (ref $locale eq 'CODE') ? $locale->() : $locale;
989 183 50       399 }
990             }
991 183 50       6414 elsif($invocant->can('parent_group') && (my $parent_group = $invocant->parent_group))
992             {
993             if(my $locale = $parent_group->locale)
994 5092         11294 {
995             return (ref $locale eq 'CODE') ? $locale->() : $locale;
996             }
997 0 0       0 }
998             else
999 0         0 {
1000             my $locale = $invocant->localizer->locale;
1001              
1002 0   0     0 if(ref $locale eq 'CODE')
1003             {
1004             return $locale->();
1005             }
1006 144 100       427  
1007             return $locale || $class->default_locale;
1008 1         8 }
1009             }
1010             else # Called as class method
1011 143         822 {
1012             if(@_)
1013             {
1014             return $invocant->default_locale(shift);
1015             }
1016              
1017             return $invocant->default_locale;
1018             }
1019 4045     4045 1 5282 }
1020              
1021              
1022 4045 50       6193 if(__PACKAGE__->localizer->auto_load_messages)
1023             {
1024 4045 100       6189 __PACKAGE__->localizer->load_all_messages;
1025             }
1026 20         46  
1027             use utf8; # The __DATA__ section contains UTF-8 text
1028 20 50       52  
1029             1;
1030 0         0  
1031              
1032             [% LOCALE en %]
1033              
1034 20         37 FIELD_LABEL = ""
1035             FIELD_DESCRIPTION = ""
1036             FIELD_REQUIRED_GENERIC = "This is a required field."
1037             FIELD_REQUIRED_LABELLED = "[1] is a required field."
1038 4025         5205  
1039             FIELD_PARTIAL_VALUE = "Incomplete value."
1040 4025 100       5741 FIELD_INVALID_GENERIC = "Value is invalid."
1041             FIELD_INVALID_LABELLED = "[label] is invalid."
1042 48 50       159  
1043             [% LOCALE de %]
1044              
1045 3977 100 66     6951 FIELD_REQUIRED_GENERIC = "Dies ist ein Pflichtfeld."
    100          
    100          
1046             FIELD_REQUIRED_LABELLED = "[1] ist ein Pflichtfeld."
1047 17 50       40  
1048             # ganze Sätze oder nur "Wert unvollständig/ungültig"?
1049 17 50       59 FIELD_PARTIAL_VALUE = "Der Wert ist unvollständig."
1050             FIELD_INVALID_GENERIC = "Der Wert ist ungültig."
1051             FIELD_INVALID_LABELLED = "[label] ist ungültig."
1052              
1053             [% LOCALE fr %]
1054 294 50       540  
1055             FIELD_REQUIRED_GENERIC = "Ce champ est obligatoire."
1056 294 50       961 FIELD_REQUIRED_LABELLED = "[1] est un champ obligatoire."
1057              
1058             FIELD_PARTIAL_VALUE = "Valeur incomplète."
1059             FIELD_INVALID_GENERIC = "Valeur invalide."
1060             FIELD_INVALID_LABELLED = "[label] est invalide."
1061 1593 50       2751  
1062             [% LOCALE bg %]
1063 1593 50       5028  
1064             FIELD_REQUIRED_GENERIC = "Това поле е задължително."
1065             FIELD_REQUIRED_LABELLED = "Полето '[1]' е задължително."
1066              
1067             FIELD_PARTIAL_VALUE = "Непълна стойност."
1068 2073         3373 FIELD_INVALID_GENERIC = "Стойността е невалидна."
1069             FIELD_INVALID_LABELLED = "Полето '[label]' е невалидно."
1070 2073 50       76447  
1071             __END__
1072 0         0  
1073             =head1 NAME
1074              
1075 2073   33     6534 Rose::HTML::Form::Field - HTML form field base class.
1076              
1077             =head1 SYNOPSIS
1078              
1079             package MyField;
1080 0 0          
1081             use base 'Rose::HTML::Form::Field';
1082 0           ...
1083              
1084             my $f = MyField->new(name => 'test', label => 'Test');
1085 0            
1086             print $f->html_field;
1087             print $f->xhtml_field;
1088              
1089       103 1   $f->input_value('hello world');
1090              
1091             $i = $f->internal_value;
1092              
1093             print $f->output_value;
1094             ...
1095              
1096 35     35   307 =head1 DESCRIPTION
  35         75  
  35         248  
1097              
1098             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.
1099              
1100             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.
1101              
1102             =head1 OVERVIEW
1103              
1104             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.
1105              
1106             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.
1107              
1108             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:
1109              
1110             +------------+
1111             / user input /
1112             +------------+
1113             |
1114             V
1115             +------------------+
1116             set -->. .
1117             . input_value . input_value()
1118             get <--. .
1119             +------------------+
1120             |
1121             V
1122             +------------------+
1123             toggle -->| input_prefilter | trim_spaces()
1124             +------------------+
1125             |
1126             V
1127             +------------------+
1128             define <-->| input_filter | input_filter()
1129             +------------------+
1130             |
1131             V
1132             +----------------------+
1133             . .
1134             get <--. input_value_filtered . input_value_filtered()
1135             . .
1136             +----------------------+
1137             |
1138             V
1139             +------------------+
1140             | inflate_value | (override in subclass)
1141             +------------------+
1142             |
1143             V
1144             +------------------+
1145             . .
1146             get <--. internal_value . internal_value()
1147             . .
1148             +------------------+
1149             |
1150             V
1151             +------------------+
1152             | deflate_value | (override in subclass)
1153             +------------------+
1154             |
1155             V
1156             +------------------+
1157             define <-->| output_filter | output_filter()
1158             +------------------+
1159             |
1160             V
1161             +------------------+
1162             . .
1163             get <--. output_value . output_value()
1164             . .
1165             +------------------+
1166              
1167              
1168             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.
1169              
1170             The purposes of the various stages of the data flow are as follows:
1171              
1172             =over 4
1173              
1174             =item B<input value>
1175              
1176             The value as it was passed to the field.
1177              
1178             =item B<input value filtered>
1179              
1180             The input value after being passed through all input filters, but before being inflated.
1181              
1182             =item B<internal value>
1183              
1184             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.
1185              
1186             =item B<output value>
1187              
1188             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.
1189              
1190             =back
1191              
1192             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.
1193              
1194             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.
1195              
1196             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.
1197              
1198             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.
1199              
1200             =head1 HIERARCHY
1201              
1202             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.
1203              
1204             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.
1205              
1206             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.
1207              
1208             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>).
1209              
1210             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.
1211              
1212             =head1 CUSTOM FIELDS
1213              
1214             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).
1215              
1216             (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.)
1217              
1218             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.
1219              
1220             Building up a library of custom fields is almost always a big win in the long run. Reuse, reuse, reuse!
1221              
1222             =head1 HTML ATTRIBUTES
1223              
1224             L<Rose::HTML::Form::Field> has the following set of valid HTML attributes.
1225              
1226             accesskey
1227             class
1228             dir
1229             id
1230             lang
1231             name
1232             onblur
1233             onclick
1234             ondblclick
1235             onfocus
1236             onkeydown
1237             onkeypress
1238             onkeyup
1239             onmousedown
1240             onmousemove
1241             onmouseout
1242             onmouseover
1243             onmouseup
1244             style
1245             tabindex
1246             title
1247             value
1248             xml:lang
1249              
1250             =head1 CONSTRUCTOR
1251              
1252             =over 4
1253              
1254             =item B<new PARAMS>
1255              
1256             Constructs a new L<Rose::HTML::Form::Field> object based on PARAMS, where
1257             PARAMS are name/value pairs. Any object method is a valid parameter name.
1258              
1259             =back
1260              
1261             =head1 OBJECT METHODS
1262              
1263             =over 4
1264              
1265             =item B<auto_invalidate_parent [BOOL]>
1266              
1267             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.
1268              
1269             See L</"parent_field"> and L</"invalidate_value"> for more information.
1270              
1271             =item B<clear>
1272              
1273             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.
1274              
1275             =item B<default VALUE>
1276              
1277             Convenience wrapper for L<default_value()|/default_value>
1278              
1279             =item B<default_value VALUE>
1280              
1281             Set the default value for the field. In the absence of a defined input value, the default value is used as the input value.
1282              
1283             =item B<deflate_value VALUE>
1284              
1285             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.
1286              
1287             =item B<description [TEXT]>
1288              
1289             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.
1290              
1291             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.
1292              
1293             It may also be useful for debugging.
1294              
1295             =item B<description_id [ID [, ARGS]]>
1296              
1297             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the description.
1298              
1299             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">.
1300              
1301             =item B<error_label [STRING]>
1302              
1303             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.
1304              
1305             If no error label is set, this method simply returns the L<label|/label>.
1306              
1307             =item B<error_label_id [ID [, ARGS]]>
1308              
1309             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">.
1310              
1311             =item B<filter [CODE]>
1312              
1313             Sets both the input filter and output filter to CODE.
1314              
1315             =item B<hidden_field>
1316              
1317             Convenience wrapper for L<hidden_fields()|/hidden_fields>
1318              
1319             =item B<hidden_fields>
1320              
1321             Returns one or more L<Rose::HTML::Form::Field::Hidden> objects that represent the hidden fields needed to encode this field's value.
1322              
1323             =item B<html>
1324              
1325             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.
1326              
1327             =item B<html_error>
1328              
1329             Returns the error text, if any, as a snippet of HTML that looks like this:
1330              
1331             <span class="error">Error text goes here</span>
1332              
1333             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.
1334              
1335             =item B<html_error_separator [STRING]>
1336              
1337             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"
1338              
1339             =item B<html_field>
1340              
1341             Returns the HTML serialization of the field.
1342              
1343             =item B<html_hidden_field>
1344              
1345             Convenience wrapper for L<html_hidden_fields()|/html_hidden_fields>
1346              
1347             =item B<html_hidden_fields>
1348              
1349             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>.
1350              
1351             =item B<html_label [ARGS]>
1352              
1353             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>.
1354              
1355             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.
1356              
1357             =item B<html_prefix [STRING]>
1358              
1359             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.
1360              
1361             =item B<html_suffix [STRING]>
1362              
1363             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.
1364              
1365             =item B<html_tag>
1366              
1367             This method is part of the L<Rose::HTML::Object> API. In this case, it simply calls L<html_field()|/html_field>.
1368              
1369             =item B<inflate_value VALUE>
1370              
1371             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.
1372              
1373             =item B<input_filter [CODE]>
1374              
1375             Get or set the input filter.
1376              
1377             =item B<input_prefilter VALUE>
1378              
1379             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.
1380              
1381             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.
1382              
1383             =item B<input_value [VALUE]>
1384              
1385             Get or set the input value.
1386              
1387             =item B<input_value_filtered>
1388              
1389             Returns the input value after passing it through the input prefilter and input filter (if any).
1390              
1391             =item B<internal_value>
1392              
1393             Returns the internal value.
1394              
1395             =item B<invalidate_output_value>
1396              
1397             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.
1398              
1399             =item B<invalidate_value>
1400              
1401             Invalidates the field's value, causing the internal and output values to be recreated the next time they are retrieved.
1402              
1403             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).
1404              
1405             =item B<is_cleared>
1406              
1407             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.
1408              
1409             =item B<is_empty>
1410              
1411             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.
1412              
1413             =item B<is_full>
1414              
1415             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.
1416              
1417             =item B<label [STRING]>
1418              
1419             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.
1420              
1421             =item B<label_id [ID]>
1422              
1423             Get or set an integer L<message|Rose::HTML::Object::Messages> id for the field label.
1424              
1425             =item B<label_object [ARGS]>
1426              
1427             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).
1428              
1429             =item B<local_name [NAME]>
1430              
1431             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.
1432              
1433             =item B<message_for_error_id PARAMS>
1434              
1435             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:
1436              
1437             =over 4
1438              
1439             =item B<msg_class CLASS>
1440              
1441             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>.
1442              
1443             =back
1444              
1445             =item B<name [NAME]>
1446              
1447             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>.
1448              
1449             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.
1450              
1451             Returns the value of the "name" HTML attribute.
1452              
1453             =item B<output_filter [CODE]>
1454              
1455             Get or set the output filter.
1456              
1457             =item B<output_value>
1458              
1459             Returns the output value.
1460              
1461             =item B<parent_field [FIELD]>
1462              
1463             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.
1464              
1465             =item B<parent_form [FORM]>
1466              
1467             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.
1468              
1469             =item B<parent_group [GROUP]>
1470              
1471             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.)
1472              
1473             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.
1474              
1475             =item B<prepare>
1476              
1477             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:
1478              
1479             package MyField;
1480             use base 'Rose::HTML::Form::Field';
1481             ...
1482             sub prepare
1483             {
1484             my($self) = shift;
1485              
1486             # Do anything that needs to be done before each use of this field
1487             ...
1488              
1489             # Call superclass implementation
1490             $self->SUPER::prepare(@_);
1491             }
1492              
1493             =item B<rank [INT]>
1494              
1495             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.
1496              
1497             =item B<required [BOOL]>
1498              
1499             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.
1500              
1501             =item B<reset>
1502              
1503             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.
1504              
1505             =item B<trim_spaces [BOOL]>
1506              
1507             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.
1508              
1509             =item B<validate>
1510              
1511             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.
1512              
1513             * If the internal value is undefined, then return false.
1514              
1515             * If the internal value is a reference to an array, and the array is empty, then return false.
1516              
1517             * If L<trim_spaces()|/trim_spaces> is true (the default) and if the internal value does not contain any non-whitespace characters, return false.
1518              
1519             If false is returned due to one of the conditions above, then L<error()|Rose::HTML::Object/error> is set to the string:
1520              
1521             $label is a required field.
1522              
1523             where C<$label> is either the field's L<label()|/label> or, if L<label()|/label> is not defined, the string "This".
1524              
1525             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.
1526              
1527             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:
1528              
1529             $label is invalid.
1530              
1531             where C<$label> is is either the field's L<label()|/label> or, if L<label()|/label> is not defined, the string "Value".
1532              
1533             The return value of the validator subroutine is then returned.
1534              
1535             If none of the above tests caused a value to be returned, then true is returned.
1536              
1537             =item B<validator [CODE]>
1538              
1539             Get or set a validator subroutine. If defined, this subroutine is called by L<validate()|/validate>.
1540              
1541             =item B<value [VALUE]>
1542              
1543             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.
1544              
1545             =item B<xhtml>
1546              
1547             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.
1548              
1549             =item B<xhtml_error>
1550              
1551             Returns the error text, if any, as a snippet of XHTML that looks like this:
1552              
1553             <span class="error">Error text goes here</span>
1554              
1555             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.
1556              
1557             =item B<xhtml_error_separator [STRING]>
1558              
1559             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"
1560              
1561             =item B<xhtml_field>
1562              
1563             Returns the XHTML serialization of the field.
1564              
1565             =item B<xhtml_hidden_field>
1566              
1567             Convenience wrapper for L<xhtml_hidden_fields()|/xhtml_hidden_fields>
1568              
1569             =item B<xhtml_hidden_fields>
1570              
1571             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>.
1572              
1573             =item B<xhtml_label [ARGS]>
1574              
1575             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>.
1576              
1577             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.
1578              
1579             =item B<xhtml_tag>
1580              
1581             This method is part of the L<Rose::HTML::Object> API. In this case, it simply calls L<xhtml_field()|/xhtml_field>.
1582              
1583             =back
1584              
1585             =head1 SUPPORT
1586              
1587             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:
1588              
1589             L<http://groups.google.com/group/rose-html-objects>
1590              
1591             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:
1592              
1593             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-HTML-Objects>
1594              
1595             There's also a wiki and other resources linked from the Rose project home page:
1596              
1597             L<http://rosecode.org>
1598              
1599             =head1 AUTHOR
1600              
1601             John C. Siracusa (siracusa@gmail.com)
1602              
1603             =head1 LICENSE
1604              
1605             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.