Maypole-FormBuilder
view release on metacpan or search on metacpan
lib/Maypole/Plugin/FormBuilder.pm view on Meta::CPAN
# this is probably not true, since CDBI::FB is careful to change an object into a class
# before building the form
die "search_form() must be called on a class, not an object" if ref $class;
# this must be set before calling _get_form_args()
$args{mode} ||= 'search'; # or do_search - both set the form action to do_search in setup_form_mode()
$args{name} ||= $r->_make_form_name( $class, $args{mode} );
$args{params} ||= $r; # $r has a suitable param() method
my $spec = $class->setup_form_mode( $r, \%args );
my $form = $class->search_form(%$spec);
$r->_add_unique_id($form);
return $form;
}
=item as_forms( %args )
%args = ( objects => $object|$arrayref_of_objects, # defaults to $r->objects
%other_form_args,
);
Generates multiple forms and returns them as a list.
You may want to reduce C<selectnum> to generate popup menus rather than multiple radiobuttons
or checkboxes ( see the C<list> template in this distribution).
=cut
sub as_forms
{
my $r = shift;
my $mode = shift if @_ % 2;
my %args = @_;
$args{mode} = $mode if $mode;
my $objects = delete $args{objects} || $r->objects;
my @objects = ref( $objects ) eq 'ARRAY' ? @$objects : ( $objects );
my @forms = map { $r->_add_unique_id($_); $_ }
map { $r->as_form( %args, entity => $_ ) }
@objects;
return @forms;
}
=item render_form_as_row( $form )
Returns a form marked up as a single row for a table.
This will probably get converted to a template some time.
Yes, it's bad XHTML - suggestions about how to do this legally would be good.
=cut
# chopped out of CGI::FormBuilder::render()
# XXX - maybe better implemented as a post-processor now
sub render_form_as_row
{
my ( $r, $form ) = @_;
my $font = $form->font;
my $fcls = $font ? '</font>' : '';
my $html;
# JavaScript validate/head functions
if ( my $sc = $form->script )
{
$html .= $sc . $form->noscript;
}
$html .= "<tr>\n" . $form->start . "\n" . $form->statetags . "\n" . $form->keepextras;
my $table = $form->table;
# Render hidden fields first
my @unhidden;
foreach my $field ( $form->field )
{
$field->type( 'text' ) if $field->type eq 'textarea';
push( @unhidden, $field ), next if $field->type ne 'hidden';
$html .= $field->tag . "\n"; # no label/etc for hidden fields
}
foreach my $field ( @unhidden )
{
next if $field->static && $field->static > 1 && ! $field->tag_value; # skip missing static vals
if ( $table )
{
$html .= $form->td . $font . $field->tag;
$html .= ' ' . $field->comment if $field->comment && ! $field->static;
$html .= ' ' . $field->message if $field->invalid;
$html .= $fcls . "</td>\n";
}
else
{
$html .= $field->label . ' ' . $field->tag . ' ';
$html .= '<br />' if $form->linebreaks;
}
}
# buttons
my $buttons = $form->reset . $form->submit;
if ( $buttons )
{
if ($table)
( run in 1.523 second using v1.01-cache-2.11-cpan-98d9bbf8dc8 )