1package Rose::HTML::Form::Field::Submit; 2 3use strict; 4 5use Carp(); 6 7use base 'Rose::HTML::Form::Field::Input'; 8 9use Rose::Object::MakeMethods::Generic 10( 11 'boolean' => 'was_submitted', 12); 13 14use Rose::HTML::Object::MakeMethods::Localization 15( 16 localized_message => 17 [ 18 qw(_value) 19 ], 20); 21 22__PACKAGE__->add_required_html_attrs( 23{ 24 type => 'submit', 25 name => '', 26}); 27 28our $VERSION = '0.616'; 29 30sub is_button { 1 } 31sub is_empty { 1 } 32 33sub hidden_fields { (wantarray) ? () : [] } 34sub html_hidden_fields { (wantarray) ? () : [] } 35 36*xhtml_hidden_fields = \&html_hidden_fields; 37 38sub clear { shift->was_submitted(0) } 39sub reset { shift->was_submitted(0) } 40 41sub image_html { shift->__image_html(0, @_) } 42sub image_xhtml { shift->__image_html(1, @_) } 43 44sub __image_html 45{ 46 my($self, $xhtml, %args) = @_; 47 48 $args{'type'} = 'image'; 49 50 my %old; 51 52 while(my($k, $v) = each(%args)) 53 { 54 if($self->html_attr_exists($k)) 55 { 56 $old{$k} = $self->html_attr($k); 57 } 58 59 $self->html_attr($k => $v); 60 } 61 62 Carp::croak("Missing src attribute") unless(length $self->html_attr('src')); 63 64 my $ret = $xhtml ? $self->xhtml : $self->html; 65 66 # Back out changes 67 foreach my $attr (keys %args) 68 { 69 if(exists $old{$attr}) 70 { 71 $self->html_attr($attr => $old{$attr}); 72 } 73 else 74 { 75 $self->delete_html_attr($attr); 76 } 77 } 78 79 return $ret; 80} 81 82sub value_message_id 83{ 84 my($self) = shift; 85 86 if(@_) 87 { 88 $self->_value_message_id(@_); 89 return $self->html_attr(value => $self->_value); 90 } 91 92 return $self->_value_message_id; 93} 94 95*value_id = \&value_message_id; 96 97sub value 98{ 99 my($self) = shift; 100 101 if(@_) 102 { 103 my $value = $self->_value(@_); 104 return $self->SUPER::value($value); 105 } 106 107 my $value = $self->html_attr('value'); 108 109 unless(defined $value) 110 { 111 return $self->html_attr(value => $self->_value); 112 } 113 114 return $value; 115} 116 117sub input_value 118{ 119 my ($self, $value) = @_; 120 no warnings 'uninitialized'; 121 $self->was_submitted($value eq $self->value); 122} 123 124sub internal_value 125{ 126 my ($self) = shift; 127 return $self->value if($self->was_submitted); 128 return undef; 129} 130 1311; 132 133__END__ 134 135=head1 NAME 136 137Rose::HTML::Form::Field::Submit - Object representation of a submit button in an HTML form. 138 139=head1 SYNOPSIS 140 141 $field = 142 Rose::HTML::Form::Field::Submit->new(name => 'run', 143 value => 'Do it!'); 144 145 print $field->html; 146 147 # or... 148 149 print $field->image_html(src => 'images/run_button.gif'); 150 151 ... 152 153=head1 DESCRIPTION 154 155L<Rose::HTML::Form::Field::Submit> is an object representation of a submit button in an HTML form. 156 157This class inherits from, and follows the conventions of, L<Rose::HTML::Form::Field>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Form::Field> documentation for more information. 158 159=head1 HTML ATTRIBUTES 160 161Valid attributes: 162 163 accept 164 accesskey 165 alt 166 checked 167 class 168 dir 169 disabled 170 id 171 ismap 172 lang 173 maxlength 174 name 175 onblur 176 onchange 177 onclick 178 ondblclick 179 onfocus 180 onkeydown 181 onkeypress 182 onkeyup 183 onmousedown 184 onmousemove 185 onmouseout 186 onmouseover 187 onmouseup 188 onselect 189 readonly 190 size 191 src 192 style 193 tabindex 194 title 195 type 196 usemap 197 value 198 xml:lang 199 200Required attributes (default values in parentheses): 201 202 name 203 type (submit) 204 205Boolean attributes: 206 207 checked 208 disabled 209 ismap 210 readonly 211 212=head1 CONSTRUCTOR 213 214=over 4 215 216=item B<new PARAMS> 217 218Constructs a new L<Rose::HTML::Form::Field::Submit> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name. 219 220=back 221 222=head1 OBJECT METHODS 223 224=over 4 225 226=item B<image_html [ARGS]> 227 228Returns the HTML serialization of the submit button using an image instead of a standard button widget (in other words, type="image"). ARGS is a list of HTML attribute name/value pairs which are temporarily set, then backed out before the method returns. (The type="image" change is also backed out.) 229 230The "src" HTML attribute must be set (either in ARGS or from an existing value for that attribute) or a fatal error will occur. 231 232=item B<image_xhtml [ARGS]> 233 234Like L<image_html()|/image_html>, but serialized to XHTML instead. 235 236=item B<is_empty> 237 238Returns true. 239 240=item B<value [VALUE]> 241 242Gets or sets the value of the "value" HTML attribute. 243 244=item B<was_submitted> 245 246Returns true if this submit button was pressed, false otherwise. 247 248=back 249 250=head1 AUTHOR 251 252John C. Siracusa (siracusa@gmail.com) 253 254=head1 LICENSE 255 256Copyright (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. 257