package HTML::FormFu::Element; use Moose; 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
s and C
s, non-blocks such as C
s and other special elements such as tables. For simple, automatic handling of fieldsets see the L setting. See L for details of L. See L for details of L, L, L, L and L. =head1 METHODS =head2 name For L element, this value is used as the C attribute which the field's value is associated with. For all elements, the L value can be useful for identifying and retrieving specific elements. =head2 is_field Return Value: boolean Returns C or C depending on whether the element is a logical form-field. This is used by L. =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 for further details. =head2 load_config_filestem Arguments: $filestem Arguments: \@filestems Like L, 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 defines where configuration files will be searched for, if an absolute path is not given to L. 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 for details. =head2 populate See L for details. =head2 stash See L for details. =head2 type Returns the C argument originally used to create the element. =head1 CHANGING DEFAULT BEHAVIOUR =head2 render_processed_value See L for details. =head2 force_errors See L 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 for details. L is an alias for L. =head2 attributes_xml =head2 attrs_xml See L for details. L is an alias for L. =head2 add_attributes =head2 add_attrs Arguments: [%attributes] Arguments: [\%attributes] Return Value: $form See L for details. L is an alias for L. =head2 add_attributes_xml =head2 add_attrs_xml See L for details. L is an alias for L. =head2 del_attributes =head2 del_attrs Arguments: [%attributes] Arguments: [\%attributes] Return Value: $form See L for details. L is an alias for L. =head2 del_attributes_xml =head2 del_attrs_xml See L for details. L is an alias for L. The following methods are shortcuts for accessing L 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 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 to render the element. =head2 prepare_id Arguments: $render See L for details. =head2 prepare_attrs Arguments: $render See L for details. =head2 render Return Value: $string =head1 INTROSPECTION =head2 parent Returns the L or L 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 object that the constraint's field is attached to. =head2 clone See L for details. =head1 ADVANCED CUSTOMISATION =head2 tt_args See L for details. =head2 render_method See L for details. =head1 CORE FORM FIELDS =over =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 OTHER CORE ELEMENTS =over =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 ELEMENT BASE CLASSES The following are base classes for other elements, and generally needn't be used directly. =over =item L =item L =item L =item L =item L =item L =item L =back =head1 REMOVED METHODS =head2 db Has been removed; use L instead. =head1 AUTHOR Carl Franks, C =head1 LICENSE This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =cut