HTML-FormFu

 view release on metacpan or  search on metacpan

lib/HTML/FormFu/Role/Element/Field.pm  view on Meta::CPAN

            push @error_container_class, $auto_class;
        }

        # auto_container_per_error_class
        my $item_class = $self->auto_container_per_error_class;

        if ( defined $item_class && length $item_class ) {
            for my $error (@errors) {
                my %string = (
                    f => sub { defined $self->form->id ? $self->form->id : '' },
                    n => sub { defined $render->{name} ? $render->{name} : '' },
                    s => sub { $error->{stage} },
                    t => sub { lc $error->{type} },
                );

                my $string = $item_class;
                $string =~ s/%([fnst])/$string{$1}->()/ge;

                push @error_container_class, $string;
            }
        }

        map {
            append_xml_attribute( $render->{error_container_attributes},
                'class', $_ )
        } uniq @error_container_class;
    }

    return;
}

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

    my $render = $self->render_data;

    return $self->_string_label($render);
}

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

    my $render = $self->render_data;

    return $self->_string_field($render);
}

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

    # field wrapper template - start

    my $html = '';

    if ( defined $render->{container_tag} ) {
        $html .= sprintf '<%s%s>',
            $render->{container_tag},
            process_attrs( $render->{container_attributes} );
    }

    if ( defined $render->{label} && $render->{label_tag} eq 'legend' ) {
        $html .= sprintf "\n%s", $self->_string_label($render);
    }

    $html .= $self->_string_errors($render);

    if (   defined $render->{label}
        && $render->{label_tag} ne 'legend'
        && !$render->{reverse_single} )
    {
        $html .= sprintf "\n%s", $self->_string_label($render);
    }

    if ( defined $render->{container_tag} ) {
        $html .= "\n";
    }

    return $html;
}

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

    # label template

    my $html = sprintf "<%s%s>%s</%s>",
        $render->{label_tag},
        process_attrs( $render->{label_attributes} ),
        $render->{label},
        $render->{label_tag},
        ;

    return $html;
}

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

    return '' if !$render->{errors};

    my $html = '';

    if ( $render->{error_container_tag} ) {
        $html .= sprintf qq{<%s%s>\n},
            $render->{error_container_tag},
            process_attrs( $render->{error_container_attributes} ),
            ;
    }

    my @error_html;
    for my $error ( @{ $render->{errors} } ) {
        push @error_html, sprintf qq{<%s%s>%s</%s>},
            $render->{error_tag},
            process_attrs( $error->{attributes} ),
            $error->{message},
            $render->{error_tag},
            ;
    }
    $html .= join "\n", @error_html;

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

    return $html;
}

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

    # field wrapper template - end

    my $html = '';

    if (   defined $render->{label}
        && $render->{label_tag} ne 'legend'
        && $render->{reverse_single} )
    {
        $html .= sprintf "\n%s", $self->_string_label($render);
    }

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

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

    if ( defined $render->{javascript} ) {
        $html .= sprintf qq{\n<script type="text/javascript">\n%s\n</script>},
            $render->{javascript},
            ;
    }

    return $html;
}

around clone => sub {
    my $orig = shift;
    my $self = shift;

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

    for my $list ( qw(
        _filters _constraints _inflators _validators _transformers
        _deflators _errors _plugins )
        )
    {
        $clone->$list( [ map { $_->clone } @{ $self->$list } ] );

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

    $clone->comment_attributes( Clone::clone( $self->comment_attributes ) );
    $clone->container_attributes( Clone::clone( $self->container_attributes ) );
    $clone->label_attributes( Clone::clone( $self->label_attributes ) );

    return $clone;
};

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormFu::Role::Element::Field - Role for all form-field elements

lib/HTML/FormFu/Role/Element/Field.pm  view on Meta::CPAN


Default Value: 'error'

If set, then the container of each field with an error will be given a
class-name based on the given pattern.

Supports L<substitutions|HTML::FormFu/ATTRIBUTE SUBSTITUTIONS>: C<%f>, C<%n>.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head3 auto_container_per_error_class

Default Value: 'error_%s_%t'

If set, then the container of each field with an error will be given a
class-name based on the given pattern.

Supports L<substitutions|HTML::FormFu/ATTRIBUTE SUBSTITUTIONS>: C<%f>, C<%n>, C<%t>, C<%s>.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head2 FORM FIELD

=head3 auto_id

If set, then the field will be given an L<id|HTML::FormFu::Element/id>
attribute, if it doesn't have one already.

E.g., setting C<< $form->auto_id('%n') >> will make each field have an ID
the same as the field's name. This makes our form config simpler, and ensures
we don't need to manually update IDs if any field names are changed.

Supports L<substitutions|HTML::FormFu/ATTRIBUTE SUBSTITUTIONS>: C<%f>, C<%n>, C<%r>.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head2 LABEL

=head3 label

Set a label to communicate the purpose of the form-field to the user.

Is an L<output accessor|HTML::FormFu/OUTPUT ACCESSORS>.

=head3 auto_label

If L<label|/label> isn't already set, the value of L</auto_label> is passed through
L<localize|HTML::FormFu/localize> to generate a label.

Supports L<substitutions|HTML::FormFu/ATTRIBUTE SUBSTITUTIONS>: C<%f>, C<%n>.

The generated string will be passed to L</localize> to create the label.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head3 label_tag

Default value: 'label'
(except L<Checkboxgroup|HTML::FormFu::Element::Checkboxgroup>)

Default value: 'legend'
(only L<Checkboxgroup|HTML::FormFu::Element::Checkboxgroup>)

Set which tag is used to wrap a L<label|/label>.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head3 label_attributes

Attributes added to the label container.

Is an L<attribute accessor|HTML::FormFu/ATTRIBUTE ACCESSOR>.

=head2 COMMENT

=head3 comment

Set a comment to be displayed along with the form-field.

Is an L<output accessor|HTML::FormFu/OUTPUT ACCESSORS>.

=head3 comment_attributes

Attributes added to the comment container.

Is an L<attribute accessor|HTML::FormFu/ATTRIBUTE ACCESSOR>.

=head3 auto_comment_class

Default Value: '%t'

If set, and if the field has a
L<comment|HTML::FormFu::Role::Element::Field/comment>, the comment tag will
be given a class-name based on the given pattern.

Supports L<substitutions|HTML::FormFu/ATTRIBUTE SUBSTITUTIONS>: C<%f>, C<%n>, C<%t>.

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head2 ERROR CONTAINER

=head3 error_container_tag

If set, and if the field has any errors, a container of this type is
wrapped around all of the field error messages.

    # Example - this would wrap each individual error in a 'li' tag,
    # with a single 'ul' tag wrapped around all the errors.

    element:
      name: foo
      error_container_tag: ul
      error_tag: li

Is an L<inheriting accessor|HTML::FormFu/INHERITING ACCESSORS>.

=head3 error_container_attributes

Set attributes on the container-tag, if L</error_container_tag> is set.

Is an L<attribute accessor|HTML::FormFu/ATTRIBUTE ACCESSOR>.



( run in 1.357 second using v1.01-cache-2.11-cpan-140bd7fdf52 )