Maypole-FormBuilder
view release on metacpan or search on metacpan
lib/Maypole/Plugin/FormBuilder.pm view on Meta::CPAN
# Need to use a separator that is legal in javascript function names (not .) and
# CSS identifiers (not _ ?). Need to use a separator in case of multiple primary columns.
# CGI::FB will still add an underscore to some identifiers though, so we'll use '_'.
my $name = join( '_', @name );
$name =~ s/[^\w]+/_/g;
return $name;
}
=item search_form
Returns a search form, via C<Class::DBI::FormBuilder::search_form()>. The C<mode>
defaults to C<do_search>.
=cut
sub search_form
{
#my ( $r, %args ) = @_;
my $r = shift;
my %args = ( @_ == 1 && ref $_[0] && ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
$args{required} ||= [];
# this has to come after setting 'required', so we don't pick up a default 'required' setting
# intended for other forms of this class
%args = $r->_merge_form_args( %args );
my $class = delete( $args{entity} ) || $r->model_class;
# 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 )
( run in 1.074 second using v1.01-cache-2.11-cpan-364913b4093 )