1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
package HTML::FormFu::Element;
use Moose;
use MooseX::Attribute::Chained;
with 'HTML::FormFu::Role::Render',
'HTML::FormFu::Role::FormAndElementMethods',
'HTML::FormFu::Role::HasParent';
use HTML::FormFu::Attribute qw(
mk_attrs
mk_attr_accessors
mk_output_accessors
mk_inherited_accessors
mk_inherited_merging_accessors
);
use HTML::FormFu::ObjectUtil qw(
load_config_file
load_config_filestem
populate
form
stash
parent
get_parent
);
use HTML::FormFu::Util qw( require_class xml_escape process_attrs );
use Clone ();
use Scalar::Util qw( refaddr weaken );
use Carp qw( croak );
use overload (
'eq' => '_string_equals',
'==' => '_object_equals',
'""' => sub { return shift->render },
bool => sub {1},
fallback => 1
);
__PACKAGE__->mk_attrs(qw( attributes ));
__PACKAGE__->mk_attr_accessors(qw( id ));
has type => ( is => 'rw', traits => ['Chained'] );
has filename => ( is => 'rw', traits => ['Chained'] );
has is_field => ( is => 'rw', traits => ['Chained'] );
has is_block => ( is => 'rw', traits => ['Chained'] );
has is_repeatable => ( is => 'rw', traits => ['Chained'] );
__PACKAGE__->mk_inherited_accessors( qw(
tt_args
render_method
config_file_path
) );
__PACKAGE__->mk_inherited_merging_accessors(qw( config_callback ));
after BUILD => sub {
my ( $self, $args ) = @_;
# TODO move to attribute 'default'
$self->attributes({});
$self->stash({});
return;
};
sub name {
my ( $self, $name ) = @_;
if ( @_ > 1 ) {
if ( $name =~ /[\.\[\]]/ ) {
croak <<'ERROR_MESSAGE';
element names may not contain periods or square brackets
see documentation on nested_names() for details
ERROR_MESSAGE
}
$self->{name} = $name;
return $self;
}
return $self->{name};
}
sub setup { }
sub get_elements { [] }
sub get_element { }
sub get_all_elements { [] }
sub get_all_element { }
sub get_fields { [] }
sub get_field { }
sub get_deflators { [] }
sub get_filters { [] }
sub get_constraints { [] }
sub get_inflators { [] }
sub get_validators { [] }
sub get_transformers { [] }
sub get_errors { [] }
sub clear_errors { }
sub pre_process { }
sub process { }
sub post_process { }
sub prepare_id { }
sub prepare_attrs { }
sub get_output_processors {
my $self = shift;
return $self->form->get_output_processors(@_);
}
sub get_output_processor {
my $self = shift;
return $self->form->get_output_processor(@_);
}
sub clone {
my ($self) = @_;
my %new = %$self;
$new{tt_args} = Clone::clone( $self->{tt_args} )
if $self->{tt_args};
$new{attributes} = Clone::clone( $self->attributes );
$new{model_config} = Clone::clone( $self->model_config );
return bless \%new, ref $self;
}
sub render_data {
return shift->render_data_non_recursive(@_);
}
sub render_data_non_recursive {
my ( $self, $args ) = @_;
my %render = (
name => xml_escape( $self->name ),
attributes => xml_escape( $self->attributes ),
type => $self->type,
filename => $self->filename,
is_field => $self->is_field,
stash => $self->stash,
parent => $self->parent,
form => sub { return shift->{parent}->form },
object => $self,
$args ? %$args : (),
);
weaken( $render{parent} );
$self->prepare_id( \%render );
$self->prepare_attrs( \%render );
return \%render;
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=head1 NAME
HTML::FormFu::Element - Element Base Class
=head1 SYNOPSIS
---
elements:
- type: Text
name: username
constraints:
- type: Required
- type: Password
name: password
constraints:
- type: Required
- type: Equal
others: repeat-password
- type: Password
name: repeat-password
- type: Submit
=head1 DESCRIPTION
Elements are the basic building block of all forms. Elements may be logical
form-fields, blocks such as C<div>s and C<fieldset>s, non-blocks such as
C<hr>s and other special elements such as tables.
For simple, automatic handling of fieldsets see the
L<HTML::FormFu/auto_fieldset> setting.
See L<HTML::FormFu/deflators> for details of
L<Deflators|HTML::FormFu::Deflator>.
See L<HTML::FormFu/FORM LOGIC AND VALIDATION> for details of
L<Filters|HTML::FormFu::Filter>,
L<Constraints|HTML::FormFu::Constraint>,
L<Inflators|HTML::FormFu::Inflator>,
L<Validators|HTML::FormFu::Validator> and
L<Transformers|HTML::FormFu::Transformer>.
=head1 METHODS
=head2 name
For L<field|HTML::FormFu::Element::_Field> element, this value is used as
the C<name> attribute which the field's value is associated with.
For all elements, the L</name> value can be useful for identifying and
retrieving specific elements.
=head2 is_field
Return Value: boolean
Returns C<true> or C<false> depending on whether the element is a logical
form-field.
This is used by L<HTML::FormFu/get_fields>.
=head1 BUILDING AN ELEMENT
=head2 load_config_file
Arguments: $filename
Arguments: \@filenames
Populate an element using a config file:
---
elements:
- type: Block
load_config_file: 'elements.yml'
See L<HTML::FormFu/load_config_file> for further details.
=head2 load_config_filestem
Arguments: $filestem
Arguments: \@filestems
Like L</load_config_file>, but you shouldn't include the file extension
in the passed string. This allows you to change your config-file type,
without having to change the code that loads the files.
=head2 config_file_path
Arguments: $directory_name
L</config_file_path> defines where configuration files will be
searched for, if an absolute path is not given to
L</load_config_file>.
Default Value: not defined
This method is a special 'inherited accessor', which means it can be set on
the form, a block element or a single element. When the value is read, if
no value is defined it automatically traverses the element's hierarchy of
parents, through any block elements and up to the form, searching for a
defined value.
=head2 config_callback
See L<HTML::FormFu/config_callback> for details.
=head2 populate
See L<HTML::FormFu/populate> for details.
=head2 stash
See L<HTML::FormFu/stash> for details.
=head2 type
Returns the C<type> argument originally used to create the element.
=head1 CHANGING DEFAULT BEHAVIOUR
=head2 render_processed_value
See L<HTML::FormFu/render_processed_value> for details.
=head2 force_errors
See L<HTML::FormFu/force_errors> for details.
=head1 ELEMENT ATTRIBUTES
See specific element types for which tag attributes are added to.
=head2 attributes
=head2 attrs
Arguments: [%attributes]
Arguments: [\%attributes]
Return Value: $form
See L<HTML::FormFu/attributes> for details.
L</attrs> is an alias for L</attributes>.
=head2 attributes_xml
=head2 attrs_xml
See L<HTML::FormFu/attributes_xml> for details.
L</attrs_xml> is an alias for L</attributes_xml>.
=head2 add_attributes
=head2 add_attrs
Arguments: [%attributes]
Arguments: [\%attributes]
Return Value: $form
See L<HTML::FormFu/add_attributes> for details.
L</add_attrs> is an alias for L</add_attributes>.
=head2 add_attributes_xml
=head2 add_attrs_xml
See L<HTML::FormFu/add_attributes_xml> for details.
L</add_attrs_xml> is an alias for L</add_attributes_xml>.
=head2 del_attributes
=head2 del_attrs
Arguments: [%attributes]
Arguments: [\%attributes]
Return Value: $form
See L<HTML::FormFu/del_attributes> for details.
L</del_attrs> is an alias for L</del_attributes>.
=head2 del_attributes_xml
=head2 del_attrs_xml
See L<HTML::FormFu/del_attributes_xml> for details.
L</del_attrs_xml> is an alias for L</del_attributes_xml>.
The following methods are shortcuts for accessing L</attributes> keys.
=head2 id
Arguments: [$id]
Return Value: $id
Get or set the element's DOM id.
Default Value: none
=head1 MODEL / DATABASE INTERACTION
See L<HTML::FormFu::Model> for further details and available models.
=head2 model_config
Arguments: \%config
=head1 RENDERING
=head2 filename
This value identifies which template file should be used by
L</render> to render the element.
=head2 prepare_id
Arguments: $render
See L<HTML::FormFu::Element::_Field/prepare_id> for details.
=head2 prepare_attrs
Arguments: $render
See L<HTML::FormFu::Element::_Field/prepare_attrs> for details.
=head2 render
Return Value: $string
=head1 INTROSPECTION
=head2 parent
Returns the L<block element|HTML::FormFu::Element::Block> or
L<form|HTML::FormFu> object that this element is attached to.
=head2 get_parent
Arguments: \%options
my $repeatable = $field->get_parent({ type => 'Repeatable' });
Traverses the parent hierarchy, returning the first parent that matches the
supplied options.
=head2 form
Returns the L<HTML::FormFu> object that the constraint's field is attached
to.
=head2 clone
See L<HTML::FormFu/clone> for details.
=head1 ADVANCED CUSTOMISATION
=head2 tt_args
See L<HTML::FormFu/tt_args> for details.
=head2 render_method
See L<HTML::FormFu/render_method> for details.
=head1 CORE FORM FIELDS
=over
=item L<HTML::FormFu::Element::Button>
=item L<HTML::FormFu::Element::Checkbox>
=item L<HTML::FormFu::Element::Checkboxgroup>
=item L<HTML::FormFu::Element::ComboBox>
=item L<HTML::FormFu::Element::ContentButton>
=item L<HTML::FormFu::Element::Date>
=item L<HTML::FormFu::Element::File>
=item L<HTML::FormFu::Element::Hidden>
=item L<HTML::FormFu::Element::Image>
=item L<HTML::FormFu::Element::Number>
=item L<HTML::FormFu::Element::Password>
=item L<HTML::FormFu::Element::Radio>
=item L<HTML::FormFu::Element::Radiogroup>
=item L<HTML::FormFu::Element::Reset>
=item L<HTML::FormFu::Element::Select>
=item L<HTML::FormFu::Element::Submit>
=item L<HTML::FormFu::Element::Textarea>
=item L<HTML::FormFu::Element::Text>
=back
=head1 OTHER CORE ELEMENTS
=over
=item L<HTML::FormFu::Element::Blank>
=item L<HTML::FormFu::Element::Block>
=item L<HTML::FormFu::Element::Fieldset>
=item L<HTML::FormFu::Element::Hr>
=item L<HTML::FormFu::Element::Label>
=item L<HTML::FormFu::Element::Multi>
=item L<HTML::FormFu::Element::Repeatable>
=item L<HTML::FormFu::Element::SimpleTable>
=item L<HTML::FormFu::Element::Src>
=back
=head1 ELEMENT BASE CLASSES
The following are base classes for other elements, and generally needn't be
used directly.
=over
=item L<HTML::FormFu::Element::_Field>
=item L<HTML::FormFu::Element::_Group>
=item L<HTML::FormFu::Element::_Input>
=item L<HTML::FormFu::Element::_MultiElement>
=item L<HTML::FormFu::Element::_MultiSelect>
=item L<HTML::FormFu::Element::_MultiText>
=item L<HTML::FormFu::Element::_NonBlock>
=back
=head1 REMOVED METHODS
=head2 db
Has been removed; use L</default_args> instead.
=head1 AUTHOR
Carl Franks, C<cfranks@cpan.org>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|