HTML-FormFu

 view release on metacpan or  search on metacpan

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

use strict;

package HTML::FormFu::Element::Checkboxgroup;
$HTML::FormFu::Element::Checkboxgroup::VERSION = '2.07';
# ABSTRACT: Group of checkbox form fields

use Moose;
use MooseX::Attribute::Chained;
extends 'HTML::FormFu::Element';

with 'HTML::FormFu::Role::Element::Group';

use HTML::FormFu::Constants qw( $EMPTY_STR );
use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
use List::Util 1.33 qw( any );

has input_type => (
    is      => 'rw',
    default => 'checkbox',
    lazy    => 1,
    traits  => ['Chained'],
);

has reverse_group => (
    is     => 'rw',
    traits => ['Chained'],
);

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

    $self->layout_field_filename('field_layout_checkboxgroup_field');
    $self->label_tag('legend');
    $self->container_tag('fieldset');
    $self->multi_value(1);
    $self->reverse_group(1);
    $self->input_type('checkbox');

    $self->layout( [ 'label', 'errors', 'field', 'comment', 'javascript', ] );

    return;
};

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

    my $form_id    = defined $self->form->id    ? $self->form->id    : '';
    my $field_name = defined $self->nested_name ? $self->nested_name : '';
    my $count      = 0;

    for my $option ( @{ $render->{options} } ) {
        if ( exists $option->{group} ) {
            for my $item ( @{ $option->{group} } ) {
                $self->_prepare_id( $item, $form_id, $field_name, \$count );
            }
        }
        else {
            $self->_prepare_id( $option, $form_id, $field_name, \$count );
        }
    }

    return;
}

sub _prepare_id {
    my ( $self, $option, $form_id, $field_name, $count_ref ) = @_;

    if ( !exists $option->{attributes}{id} && defined $self->auto_id ) {
        my %string = (
            f => $form_id,
            n => $field_name,
        );

        my $id = $self->auto_id;
        $id =~ s/%([fn])/$string{$1}/g;
        $id =~ s/%c/ ++$$count_ref /gex;
        $id =~ s/%v/ $option->{value} /gex;

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

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

    # label "for" attribute
    if (   exists $option->{label}
        && exists $option->{attributes}{id}
        && !exists $option->{label_attributes}{for} )
    {
        $option->{label_attributes}{for} = $option->{attributes}{id};
    }



( run in 2.179 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )