Class:...AsForm(3pm)

πŸ“¦
version 2.42 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Class::DBI::AsForm β€” bestie, this one makes HTML form elements for your database columns. It's giving "automatic form factory" and honestly? iconic. ✨


🌸 SYNOPSIS 🌸

package Music::CD;
use Class::DBI::AsForm;
use base 'Class::DBI';
use CGI;
...

sub create_or_edit {
    my $class = shift;
    my %cgi_field = $class->to_cgi;
    return start_form,
           (map { "<b>$_</b>: ". $cgi_field{$_}->as_HTML." <br>" }
                $class->Columns),
           end_form;
}

# <form method="post"...>
# Title: <input type="text" name="Title" /> <br>
# Artist: <select name="Artist">
#           <option value=1>Grateful Dead</option>
#           ...
#         </select>
# ...
# </form>

Like… look at that. You write basically nothing and you get a whole form. The way that $class->Columns just hands you the fields? Obsessed. 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the vibe: this module helps you generate HTML forms for creating new database rows or editing existing rows. It takes the column names in a database table and maps them to HTML form elements that actually fit the schema. No cap.

The glow-up details:

  • Big text fields? Boom, they become textareas. πŸ“
  • Fields with a has-a relationship to other Class::DBI tables? They become select drop-downs, populated with objects from the joined class. πŸ”₯

It just knows. Main character energy fr fr.


πŸ’… METHODS πŸ’…

This module is a mix-in, meaning it adds two extra methods to your Class::DBI-derived class. Two. That's it. Lowkey perfect.

✨ to_cgi

This returns a hash mapping all the column names of the class to HTML::Element objects representing form widgets.

(So you get actual objects, not just strings β€” you can poke at them, style them, do whatever. Very demure, very mindful. πŸ’–)

✨ to_field($field [, $how])

This maps an individual column to a form element. The how argument lets you force the field type into one of textfield, textarea or select. Use it if you want to avoid the automatic detection of has-a relationships β€” sometimes you just wanna be in control, and that's valid. πŸ‘‘


πŸŽ€ CHANGES πŸŽ€

Version 1.x of this module returned raw HTML instead of HTML::Element objects, which made it harder to manipulate the HTML before sending it out. (Messy. We don't do that anymore. πŸ’€)

If you depend on the old behaviour, set $Class::DBI::AsForm::OLD_STYLE to a true value. Consider it your little time machine. πŸ•°οΈ

πŸ‘‘ MAINTAINER πŸ‘‘

Tony Bowden


🌈 ORIGINAL AUTHOR 🌈

Simon Cozens


😭 BUGS and QUERIES 😭

Please direct all correspondence regarding this module to:

bug-Class-DBI-AsForm@rt.cpan.org

Like, all of it. Everything. Send it there. πŸ’Œ


Copyright 2003-2004 by Simon Cozens / Tony Bowden

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


⭐ SEE ALSO ⭐

  • Class::DBI
  • Class::DBI::FromCGI
  • HTML::Element

And that's the whole page, bestie β€” this is Class:...AsForm(3pm) from the User Contributed Perl Documentation, sitting pretty at perl v5.36.0 dated 2022-12-07. Go build some forms, you absolute legend. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from