HTML::FormFu::Element::Block - Block element


HTML-FormFu documentation Contained in the HTML-FormFu distribution.

Index


Code Index:

NAME

Top

HTML::FormFu::Element::Block - Block element

SYNOPSIS

Top

    ---
    elements: 
      - type: Block
        elements: 
          - type: Text
            name: foo

      - type: Block
        tag: span
        content: Whatever

DESCRIPTION

Top

Block element which may contain other elements.

METHODS

Top

tag

Specifies which tag name should be used to render the block.

Default Value: 'div'

content

If content is set, it is used as the block's contents, and any attached elements are ignored.

content_xml

Arguments: $string

If you don't want the content to be XML-escaped, use the content_xml method instead of content.

content_loc

Arguments: $localization_key

To set the content to a localized string, set content_loc to a key in your L10N file instead of using content.

elements

See elements in HTML::FormFu for details.

element

See element in HTML::FormFu for details.

deflators

See deflators in HTML::FormFu for details.

deflator

See deflator in HTML::FormFu for details.

filters

See filters in HTML::FormFu for details.

filter

See filter in HTML::FormFu for details.

constraints

See constraints in HTML::FormFu for details.

constraint

See constraint in HTML::FormFu for details.

inflators

See inflators in HTML::FormFu for details.

inflator

See inflator in HTML::FormFu for details.

validators

See validators in HTML::FormFu for details.

validator

See validator in HTML::FormFu for details.

transformers

See transformers in HTML::FormFu for details.

transformer

See transformer in HTML::FormFu for details.

CSS CLASSES

Top

auto_id

See auto_id in HTML::FormFu for details.

auto_block_id

Arguments: [$string]

If set, the Block will be given an auto-generated id attribute, if it doesn't have one already.

The following character substitution will be performed: %f will be replaced by $form->id, %r will be replaced by $block->repeatable_count.

Default Value: not defined

Unlike most other auto_* methods, this is not an 'inherited accessor'.

auto_label

See auto_label in HTML::FormFu for details.

auto_error_class

See auto_error_class in HTML::FormFu for details.

auto_error_message

See auto_error_message in HTML::FormFu for details.

auto_constraint_class

See auto_constraint_class in HTML::FormFu for details.

auto_inflator_class

See auto_inflator_class in HTML::FormFu for details.

auto_validator_class

See auto_validator_class in HTML::FormFu for details.

auto_transformer_class

See auto_transformer_class in HTML::FormFu for details.

default_args

See default_args in HTML::FormFu for details.

RENDERING

Top

start

end

INTROSPECTION

Top

get_elements

See get_elements in HTML::FormFu for details.

get_element

See get_element in HTML::FormFu for details.

get_all_elements

See get_all_elements in HTML::FormFu for details.

get_fields

See get_fields in HTML::FormFu for details.

get_field

See get_field in HTML::FormFu for details.

get_deflators

See get_deflators in HTML::FormFu for details.

get_deflator

See get_deflator in HTML::FormFu for details.

get_filters

See get_filters in HTML::FormFu for details.

get_filter

See get_filter in HTML::FormFu for details.

get_constraints

See get_constraints in HTML::FormFu for details.

get_constraint

See get_constraint in HTML::FormFu for details.

get_inflators

See get_inflators in HTML::FormFu for details.

get_inflator

See get_inflator in HTML::FormFu for details.

get_validators

See get_validators in HTML::FormFu for details.

get_validator

See get_validator in HTML::FormFu for details.

get_transformers

See get_transformers in HTML::FormFu for details.

get_transformer

See get_transformer in HTML::FormFu for details.

get_errors

See get_errors in HTML::FormFu for details.

clear_errors

See clear_errors in HTML::FormFu for details.

SEE ALSO

Top

Base-class for HTML::FormFu::Element::Fieldset.

Is a sub-class of, and inherits methods from HTML::FormFu::Element

HTML::FormFu

REMOVED METHODS

Top

element_defaults

Has been removed; use default_args instead.

AUTHOR

Top

Carl Franks, cfranks@cpan.org

LICENSE

Top

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.


HTML-FormFu documentation Contained in the HTML-FormFu distribution.

package HTML::FormFu::Element::Block;
use Moose;

extends 'HTML::FormFu::Element';

with 'HTML::FormFu::Role::CreateChildren',
     'HTML::FormFu::Role::GetProcessors',
     'HTML::FormFu::Role::ContainsElements',
     'HTML::FormFu::Role::ContainsElementsSharedWithField',
     'HTML::FormFu::Role::FormAndBlockMethods';

use HTML::FormFu::Constants qw( $EMPTY_STR );
use HTML::FormFu::Util qw( _get_elements xml_escape process_attrs );
use Clone ();
use List::MoreUtils qw( uniq );
use Carp qw( croak );

has tag                  => ( is => 'rw', traits => ['Chained'] );
has nested_name          => ( is => 'rw', traits => ['Chained'] );
has original_nested_name => ( is => 'rw', traits => ['Chained'] );
has auto_block_id        => ( is => 'rw', traits => ['Chained'] );

has _elements => (
    is      => 'rw',
    default => sub { [] },
    lazy    => 1,
    isa     => 'ArrayRef',
);

__PACKAGE__->mk_output_accessors(qw( content ));

__PACKAGE__->mk_inherited_accessors( qw(
        auto_id auto_label auto_error_class auto_error_message
        auto_constraint_class auto_inflator_class auto_validator_class
        auto_transformer_class render_processed_value force_errors
        repeatable_count
        locale
) );

*elements     = \&element;
*constraints  = \&constraint;
*deflators    = \&deflator;
*filters      = \&filter;
*inflators    = \&inflator;
*validators   = \&validator;
*transformers = \&transformer;
*plugins      = \&plugin;

after BUILD => sub {
    my ( $self, $args ) = @_;

    $self->filename( 'block' );
    $self->tag(      'div' );
    $self->is_block( 1 );
    
    return;
};

sub _single_plugin {
    my ( $self, $arg ) = @_;

    if ( !ref $arg ) {
        $arg = { type => $arg };
    }
    elsif ( ref $arg eq 'HASH' ) {
        $arg = {%$arg};    # shallow clone
    }
    else {
        croak 'invalid args';
    }

    my @names = map { ref $_ ? @$_ : $_ }
        grep {defined} ( delete $arg->{name}, delete $arg->{names} );

    if ( !@names ) {
        @names = uniq
            grep {defined}
            map  { $_->nested_name } @{ $self->get_fields };
    }

    croak "no field names to add plugin to" if !@names;

    my $type = delete $arg->{type};

    my @return;

    for my $x (@names) {
        for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
            my $new = $field->_require_plugin( $type, $arg );
            push @{ $field->_plugins }, $new;
            push @return, $new;
        }
    }

    return @return;
}

sub pre_process {
    my ($self) = @_;

    map { $_->pre_process } @{ $self->_elements };

    return;
}

sub process {
    my ($self) = @_;

    map { $_->process } @{ $self->_elements };

    return;
}

sub post_process {
    my ($self) = @_;

    map { $_->post_process } @{ $self->_elements };

    return;
}

sub render_data {
    my $self = shift;

    my $render = $self->render_data_non_recursive( { @_ ? %{ $_[0] } : () } );

    $render->{elements} = [ map { $_->render_data } @{ $self->_elements } ];

    return $render;
}

sub render_data_non_recursive {
    my ( $self, $args ) = @_;

    my $render = $self->SUPER::render_data_non_recursive( {
            tag     => $self->tag,
            content => xml_escape( $self->content ),
            $args ? %$args : (),
        } );

    return $render;
}

sub prepare_id {
    my ( $self, $render ) = @_;

    if (  !defined $render->{attributes}{id}
        && defined $self->auto_block_id
        && length $self->auto_block_id )
    {
        my $form_name
            = defined $self->form->id
            ? $self->form->id
            : $EMPTY_STR;

        my %string = (
            f => $form_name,
        );

        my $id = $self->auto_block_id;
        $id =~ s/%([f])/$string{$1}/g;

        if ( defined( my $count = $self->repeatable_count ) ) {
            $id =~ s/%r/$count/g;
        }

        $render->{attributes}{id} = $id;
    }

    return;
}

sub string {
    my ( $self, $args ) = @_;

    $args ||= {};

    my $render
        = exists $args->{render_data}
        ? $args->{render_data}
        : $self->render_data_non_recursive;

    # start_block template

    my $html = '';

    if ( defined $render->{tag} ) {
        $html .= sprintf "<%s%s>",
            $render->{tag},
            process_attrs( $render->{attributes} ),
            ;
    }

    if ( defined $render->{legend} ) {
        $html .= sprintf "\n<legend>%s</legend>", $render->{legend};
    }

    # block template

    $html .= "\n";

    if ( defined $render->{content} ) {
        $html .= sprintf "%s\n", $render->{content};
    }
    else {
        for my $elem ( @{ $self->get_elements } ) {

            # call render, so that child elements can use a different renderer
            my $elem_html = $elem->render;

            # skip Blank fields
            if ( length $elem_html ) {
                $html .= $elem_html . "\n";
            }
        }
    }

    # end_block template

    if ( defined $render->{tag} ) {
        $html .= sprintf "</%s>", $render->{tag};
    }

    return $html;
}

sub start {
    my ($self) = @_;

    return $self->tt( {
            filename    => 'start_block',
            render_data => $self->render_data_non_recursive,
        } );
}

sub end {
    my ($self) = @_;

    return $self->tt( {
            filename    => 'end_block',
            render_data => $self->render_data_non_recursive,
        } );
}

sub clone {
    my $self = shift;

    my $clone = $self->SUPER::clone(@_);

    $clone->_elements( [ map { $_->clone } @{ $self->_elements } ] );

    map { $_->parent($clone) } @{ $clone->_elements };

    $clone->default_args( Clone::clone( $self->default_args ) );

    return $clone;
}

__PACKAGE__->meta->make_immutable;

1;

__END__