HTML-Widget

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

        - $result has new method elements_ref() which returns an arrayref.

1.09 2006-09-21
        - Embedding completely refactored by Michael Gray.
        - $w->embed( $e, @widgets) embeds into the supplied element.
        - New Fieldset element.
        - Can add elements to Fieldset elements, as an alternative to using 
          embed.
        - New $w->xhtml_strict accessor. When true, doesn't allow non-block 
          elements at the top-level of a form.
        - New $w->legend accessor. Sets the label for the top-level fieldset.
        - process() is now called for embedded widgets.
        - After form submission, $result->as_xml() no longer uses an elements' 
          default value if that field wasn't submitted.
        - Removed In constraint change added in v1.08 (in which validate 
          automatically passed an empty in() list)
        - Select elements no longer automatically get an implicit In constraint 
          added. Set constrain_options() to true to get the old behaviour.
        - RadioGroup elements no longer automatically get an implicit In 
          constraint added. Set constrain_values to true to get the old 
          behaviour.

examples/big.pl  view on Meta::CPAN

use HTML::Widget;
use Test::MockObject;

my $w1 = HTML::Widget->new('widget1')->legend('widget1');
my $w2 = HTML::Widget->new('widget2');

$w1->element( 'Checkbox', 'checkbox1' )->label('Checkbox1');
$w1->element( 'Checkbox', 'checkbox2' )->label('Checkbox3');
$w1->element( 'Checkbox', 'checkbox3' )->label('Checkbox2');

$w1->element( 'Radio', 'radio' )->label('Radio1');
$w1->element( 'Radio', 'radio' )->label('Radio2');
$w1->element( 'Radio', 'radio' )->label('Radio3');

examples/simple.css  view on Meta::CPAN

}
form {
    width: 50%;
    margin: 1em auto;
}
.widget_fieldset {
    padding: 0.5em 0;
    border: 1px solid #c0c0c0;
    background: #f0f0f0;
}
legend {
    font-weight: bold;
    font-style: italic;
    color: #333;
    margin: 0;
    padding: 0.1em 0.5em;
}
label, .radiogroup_fieldset {
    padding: 0 1em;
    margin: 0em;
    margin-top: 0.5em;

lib/HTML/Widget.pm  view on Meta::CPAN

the L<mailing-list|/SUPPORT>.

=head1 SYNOPSIS

    use HTML::Widget;

    # Create a widget
    my $w = HTML::Widget->new('widget')->method('get')->action('/');

    # Add a fieldset to contain the elements
    my $fs = $w->element( 'Fieldset', 'user' )->legend('User Details');

    # Add some elements
    $fs->element( 'Textfield', 'age' )->label('Age')->size(3);
    $fs->element( 'Textfield', 'name' )->label('Name')->size(60);
    $fs->element( 'Submit', 'ok' )->value('OK');

    # Add some constraints
    $w->constraint( 'Integer', 'age' )->message('No integer.');
    $w->constraint( 'Not_Integer', 'name' )->message('Integer.');
    $w->constraint( 'All', 'age', 'name' )->message('Missing value.');

lib/HTML/Widget.pm  view on Meta::CPAN

    $e->comment('(Required)');
    $e->label('Foo');
    $e->checked('checked');
    $e->value('bar');

Add a standard checkbox element.

=item L<HTML::Widget::Element::Fieldset>

    my $e = $widget->element( 'Fieldset', 'foo' );
    $e->legend('Personal details');
    $e->element('Textfield', 'name');
    $e->element('Textarea', 'address');

Adds a nested fieldset element, which can contain further elements.

=item L<HTML::Widget::Element::Hidden>

    my $e = $widget->element( 'Hidden', 'foo' );
    $e->value('bar');

lib/HTML/Widget.pm  view on Meta::CPAN

=head2 indicator

Arguments: $field_name

Return Value: $field_name

Set/Get a boolean field. This is a convenience method for the user, so they 
can keep track of which of many Widget objects were submitted. It is also
used by L<Catalyst::Plugin::HTML::Widget>

=head2 legend

Arguments: $legend

Return Value: $legend

Set/Get a legend for this widget. This tag is used to label the fieldset. 

=cut

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

    croak "'legend' not permitted at top level in xhtml_strict mode"
        if $self->xhtml_strict;

    my $top_level = $self->_get_implicit_subcontainer;
    unless ( $top_level->can('legend') ) {
        croak "implicit subcontainer does not support 'legend'";
    }

    $top_level->legend($legend);
    return $self;
}

=head2 merge

Arguments: @widgets

Arguments: $element, @widgets

Merge elements, constraints and filters from other widgets, into this one. The

lib/HTML/Widget.pm  view on Meta::CPAN


Arguments: $bool

Return Value: $bool

When C<true>, it is an error to have any element at the top-level of the 
widget which is not derived from L<HTML::Widget::Element::Block>. 
Currently, the only valid element supplied is the  
L<HTML::Widget::Element::Fieldset>.

When C<true>, the top-level widget may not have a L/legend>.

=head1 Frequently Asked Questions (FAQ)

=head2 How do I add an onSubmit handler to my form?

    $widget->attributes( onsubmit => $javascript );

See L<HTML::Widget/attributes>.

=head2 How do I add an onChange handler to my form field?

lib/HTML/Widget/Element/Fieldset.pm  view on Meta::CPAN

package HTML::Widget::Element::Fieldset;

use warnings;
use strict;
use base 'HTML::Widget::Element::Block';
use NEXT;

__PACKAGE__->mk_accessors('legend');

=head1 NAME

HTML::Widget::Element::Fieldset - Fieldset Element

=head1 SYNOPSIS

    my $fs = $widget->element( 'Fieldset', 'address' );
    $fs->element( 'Textfield', 'street' );
    $fs->element( 'Textfield', 'town' );

lib/HTML/Widget/Element/Fieldset.pm  view on Meta::CPAN

=head1 METHODS

=head2 new

=cut

sub new {
    return shift->NEXT::new(@_)->type('fieldset')->class('widget_fieldset');
}

=head2 legend

Set a legend for this fieldset.

=cut

sub _pre_content_elements {
    my ( $self, $w ) = @_;
    return () unless $self->legend;

    my %args;
    $args{id} = $self->id($w) . "_legend" if defined $self->name;
    my $l = HTML::Element->new( 'legend', %args );

    $l->push_content( $self->legend );
    return ($l);
}

=head1 SEE ALSO

L<HTML::Widget::Element>

=head1 AUTHOR

Michael Gray, C<mjg@cpan.org>

lib/HTML/Widget/Element/RadioGroup.pm  view on Meta::CPAN


use warnings;
use strict;
use base 'HTML::Widget::Element';

*value = \&checked;

__PACKAGE__->mk_accessors(
    qw/
        comment label values labels comments checked _current_subelement
        constrain_values legend retain_default/
);

=head1 NAME

HTML::Widget::Element::RadioGroup - Radio Element grouping

=head1 SYNOPSIS

    my $e = $widget->element( 'RadioGroup', 'foo' );
    $e->comment('(Required)');

lib/HTML/Widget/Element/RadioGroup.pm  view on Meta::CPAN

=head1 METHODS

=head2 comment

Add a comment to this Element.

=head2 label

This label will be placed next to your Element.

=head2 legend

Because the RadioGroup is placed in a C<fieldset> tag, you can also set a 
</legend> value. Note, however, that if you want the RadioGroup to be styled 
the same as other elements, the L</label> setting is recommended.

=head2 values

List of form values for radio checks. 
Will also be used as labels if not otherwise specified via L<labels>.

=head2 checked

=head2 value

lib/HTML/Widget/Element/RadioGroup.pm  view on Meta::CPAN

        $label;
    } @values;

    $self->_current_subelement(undef);

    my $fieldset = HTML::Element->new('fieldset');
    $fieldset->attr( class => 'radiogroup_fieldset' );

    my $outer_id = $self->attributes->{id} || $self->id($w);

    if ( defined $self->legend ) {
        my $legend = HTML::Element->new('legend');
        $legend->attr( class => 'radiogroup_legend' );
        $legend->push_content( $self->legend );
        $fieldset->push_content($legend);
    }

    # don't pass commment to mk_label, we'll handle it ourselves
    my $l = $self->mk_label( $w, $self->label, undef, $errors );
    if ($l) {
        $l->tag('span');
        $l->attr( for   => undef );
        $l->attr( class => 'radiogroup_label' );
    }

lib/HTML/Widget/Result.pm  view on Meta::CPAN

}

=head2 id

Arguments: $id

Return Value: $id

Contains the widget id.

=head2 legend

Arguments: $legend

Return Value: $legend

Contains the legend.

=head2 method

Arguments: $method

Return Value: $method

Contains the form method.

=head2 param

t/04basic.t  view on Meta::CPAN

use HTML::Widget;
use lib 't/lib';
use HTMLWidget::TestLib;

my $w = HTML::Widget->new->method('post')->action('/foo/bar');

$w->element( 'Textfield', 'age' )->label('Age')->size(3);
$w->element( 'Textfield', 'name' )->label('Name')->size(60);
$w->element( 'Submit',    'ok' )->value('OK');

$w->legend('Fool');

$w->constraint( 'Integer', 'age' )->message('No integer.');
$w->constraint( 'Length',  'age' )->min(1)->max(3)->message('Wrong length.');
$w->constraint( 'Range',   'age' )->min(22)->max(24)->message('Wrong range.');
$w->constraint( 'Regex',   'age' )->regex(qr/\D+/)
    ->message('Contains digit characters.');
$w->constraint( 'Not_Integer', 'name' );
$w->constraint( 'All', 'age', 'name' )->message('Missing value.');

# Without query
{
    my $f = $w->result;
    is( $f->as_xml, <<EOF, 'XML output is form' );
<form action="/foo/bar" id="widget" method="post"><fieldset class="widget_fieldset"><legend id="widget_legend">Fool</legend><label for="widget_age" id="widget_age_label">Age<input class="textfield" id="widget_age" name="age" size="3" type="text" /></...
EOF
}

# With mocked basic query
{
    my $query = HTMLWidget::TestLib->mock_query( {
            age  => 23,
            name => 'sri',
            ok   => 'OK',
        } );

t/04basic.t  view on Meta::CPAN


    my @errors = $f->errors;
    is( $errors[0]->name, 'age', 'Expected error' );

    is( $errors[0],
        'Contains digit characters.',
        'Field contains digit characters'
    );

    is( "$f", <<EOF, 'XML output is filled out form' );
<form action="/foo/bar" id="widget" method="post"><fieldset class="widget_fieldset"><legend id="widget_legend">Fool</legend><label class="labels_with_errors" for="widget_age" id="widget_age_label">Age<span class="fields_with_errors"><input class="tex...
EOF
}

# Embed
{
    my $w2 = HTML::Widget->new('foo')->action('/foo');
    my $w3 = HTML::Widget->new('bar');

    $w3->element( 'Textfield', 'baz' );

    $w2->embed($w);
    $w2->embed($w3);

    my $f = $w2->process;
    is( $f->as_xml, <<EOF, 'XML output is form' );
<form action="/foo" id="foo" method="post"><fieldset class="widget_fieldset" id="foo_widget"><legend id="foo_widget_legend">Fool</legend><label for="foo_widget_age" id="foo_widget_age_label">Age<input class="textfield" id="foo_widget_age" name="age" ...
EOF
}

# Merge
{
    my $w2 = HTML::Widget->new('foo')->action('/foo');
    my $w3 = HTML::Widget->new('bar');

    $w3->element( 'Textfield', 'baz' );

t/10no_name.t  view on Meta::CPAN


    is( "$f", <<EOF, 'XML output is filled out form' );
<form id="widget" method="post"><fieldset class="widget_fieldset"><input class="textfield" id="widget_foo" name="foo" type="text" /></fieldset></form>
EOF
}

{
    my $w = HTML::Widget->new;

    $w->element( 'Fieldset', 'foo' )
        ->legend( 'the legend of foo' )
        ->element('Fieldset')
            ->legend( 'the legend of blank' )
            ->element( 'Fieldset', 'baz' )
                ->legend( 'the legend of baz' )
                ->element( 'Textfield', 'bar' );

    my $f = $w->process;

    is( "$f", <<EOF, 'XML output is filled out form' );
<form id="widget" method="post"><fieldset class="widget_fieldset" id="widget_foo"><legend id="widget_foo_legend">the legend of foo</legend><fieldset class="widget_fieldset"><legend>the legend of blank</legend><fieldset class="widget_fieldset" id="wid...
EOF
}

t/element_block.t  view on Meta::CPAN


use HTML::Widget;
use lib 't/lib';
use HTMLWidget::TestLib;

my $w = HTML::Widget->new;

my $e = $w->element( 'Block', 'foo' );
$e->element( 'Textfield', 'bar' )->value('bar')->label('Bar');

my $fs = $e->element( 'Fieldset', 'fs' )->legend('FS');
$fs->element( 'Textfield', 'baz' );

my $fs2 = $e->element( 'Fieldset', 'fs2' );
$fs2->element( 'Textfield', 'bartwo' );
$fs2->element( 'Textfield', 'baztwo' );

my $fsn = $fs2->element( 'Fieldset', 'fsnest' );
$fsn->element( 'Textfield', 'barnest' )->value('Barnest');

# Not completely sure if NullContainers should be used for real,
# but test them anyway as they're the base for Block.
my $nc = $e->element( 'NullContainer', 'nc' );
$nc->element( 'Textfield', 'norp' );

# Without query
{
    my $f = $w->process;
    is( "$f", <<EOF, 'XML output' );
<form id="widget" method="post"><div id="widget_foo"><label for="widget_foo_bar" id="widget_foo_bar_label">Bar<input class="textfield" id="widget_foo_bar" name="bar" type="text" value="bar" /></label><fieldset class="widget_fieldset" id="widget_foo_f...
EOF
}

# With mocked basic query - okay
{
    my $query = HTMLWidget::TestLib->mock_query( {
            bar     => 'yada',
            baz     => '23',
            bartwo  => 'ping',
            baztwo  => '18',
            barnest => 'yellow',
        } );

    my $f = $w->process($query);
    is( "$f", <<EOF, 'XML output with query' );
<form id="widget" method="post"><div id="widget_foo"><label for="widget_foo_bar" id="widget_foo_bar_label">Bar<input class="textfield" id="widget_foo_bar" name="bar" type="text" value="yada" /></label><fieldset class="widget_fieldset" id="widget_foo_...
EOF
}

# With mocked basic query - errors
$w->constraint( 'Integer', 'bar', 'baz', 'bartwo', 'baztwo' );
{
    my $query = HTMLWidget::TestLib->mock_query( {
            bar    => 'yada',
            baz    => '23',
            bartwo => 'ping',
            baztwo => '18',
            norp   => 'Nil',
        } );

    my $f = $w->process($query);
    is( "$f", <<EOF, 'XML output with query and errors' );
<form id="widget" method="post"><div id="widget_foo"><label class="labels_with_errors" for="widget_foo_bar" id="widget_foo_bar_label">Bar<span class="fields_with_errors"><input class="textfield" id="widget_foo_bar" name="bar" type="text" value="yada"...
EOF
}

# Introspection
my @el = $e->get_elements();
is( scalar(@el), 4, 'foo has 4 els' );
isa_ok( $el[0], 'HTML::Widget::Element::Textfield', 'foo 1st el is textfield' );
isa_ok( $el[1], 'HTML::Widget::Element::Fieldset',  'foo 2nd el is fieldset' );
my @fsl = $el[1]->get_elements();
is( scalar(@fsl), 1, 'fs has 1' );

t/element_radiogroup.t  view on Meta::CPAN

# With mocked basic query
{
    my $query = HTMLWidget::TestLib->mock_query( { bar => 'opt2' } );

    my $f = $w->process($query);
    is( "$f", <<EOF, 'XML output is filled out form' );
<form id="widget" method="post"><fieldset class="widget_fieldset"><fieldset class="radiogroup_fieldset" id="widget_bar"><span class="radiogroup"><label for="widget_bar_1" id="widget_bar_1_label"><input class="radio" id="widget_bar_1" name="bar" type=...
EOF
}

# With legend
$e->legend('Select One');
{
    my $f = $w->process;
    is( "$f", <<EOF, 'XML output is filled out form (label)' );
<form id="widget" method="post"><fieldset class="widget_fieldset"><fieldset class="radiogroup_fieldset" id="widget_bar"><legend class="radiogroup_legend">Select One</legend><span class="radiogroup"><label for="widget_bar_1" id="widget_bar_1_label"><i...
EOF
}

# With label
$e->legend(undef);
$e->label('Choose');

{
    my $f = $w->process;
    is( "$f", <<EOF, 'XML output is filled out form (label)' );
<form id="widget" method="post"><fieldset class="widget_fieldset"><fieldset class="radiogroup_fieldset" id="widget_bar"><span class="radiogroup_label" id="widget_bar_label">Choose</span><span class="radiogroup"><label for="widget_bar_1" id="widget_ba...
EOF
}

# With comment too



( run in 2.081 seconds using v1.01-cache-2.11-cpan-49f99fa48dc )