HTML::...::DBIC(3pm)
π HTML::...::DBIC β the manual, but make it a group chat π
okay bestie, so this page you're reading is the manpage for HTML::...::DBIC, and the actual thing it documents is HTML::FormHandler::TraitFor::Model::DBIC β a model role that interfaces with DBIx::Class. yes that's the NAME section, we're doing it, let's go π¦
β¨ VERSION β¨
version 0.29 β that's the one, that's the version, don't @ me π
πΈ SYNOPSIS πΈ
two ways to vibe with this, pick your fighter:
Option 1: subclass your form from HTML::FormHandler::Model::DBIC:
package MyApp::Form::User;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Model::DBIC';
Option 2: apply it as a role to your FormHandler class:
package MyApp::Form::User;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
with 'HTML::FormHandler::TraitFor::Model::DBIC';
either way you're slaying, promise π«Ά
π DESCRIPTION π
so this is a separate DBIx::Class model role, made specifically for HTML::FormHandler. it will:
- handle normal DBIC column accessors β¨
- handle a whole bunch of DBIC relationships π«
- save your form fields automatically to the database, no manual labour, periodt π
the distribution also contains a form generator, HTML::FormHandler::Generator::DBIC. and there's an example app on github at http://github.com/gshank/formhandler-example if you wanna see it living its truth π
oh and also β HTML::FormHandler::TraitFor::DBICFields can auto-generate forms straight from a DBIC result. iconic behaviour:
my $book = $schema->resultset('Book')->find(1);
my $form = HTML::FormHandler::Model::DBIC->new_with_traits(
traits => ['HTML::FormHandler::TraitFor::DBICFields'],
field_list => [ 'submit' => { type => 'Submit', value => 'Save', order => 99 } ],
item => $book );
this model supports using DBIx::Class result_source accessors just as if they were standard columns. forms that need custom updating usually subclass, or use an 'around' method modifier on the 'update_model' method β very demure, very mindful π
π§ Two Ways To Get A Valid DBIC Model π§
The first way is to set:
item_id (primary key)
item_class (source name)
schema
the 'item_class' is usually set in the form class:
# Associate this form with a DBIx::Class result class
has '+item_class' => ( default => 'User' ); # 'User' is the DBIC source_name
and 'item_id' + 'schema' must be passed in when the form is used in your controller:
$form->process( item_id => $id, schema => $c->model('DB')->schema,
params => $c->req->params );
β οΈ heads up: if the item_id is not defined, then a new record will be created. no cap, that's just what happens.
The second way is to pass in a DBIx::Class row, aka the 'item':
$form->process( item => $row, params => $c->req->params );
and 'item_id', 'item_class', and 'schema' all get derived from the 'item' πͺ. for a brand new row (like on a 'create'), you can use new_result:
my $item = $c->model('DB::Book')->new_result({});
$form->process( item => $item, params => $c->req->params );
the accessor names of the fields in your form should match column, relationship, or accessor names in your DBIx::Class result source. usually the field name and the accessor are the same, but lowkey they may be different. it's giving "same same but different" π
π DBIC Relationships π
π belongs_to
Single Select fields will handle 'belongs_to' relationships, where the related table is used to construct a selection list from the database. cute, right? π
π many_to_many
Multiple Select fields use a 'many_to_many' pseudo-relation to retrieve the selection list from the database:
has_field 'roles' => (
type => 'Multiple',
label_column => 'role',
);
you need to supply 'label_column' to indicate which column should be used as label. no label_column, no label. that's the rule π
a Compound field can represent a single relation. a Repeatable field will map onto a multiple relationship. obsessed with the symmetry honestly β¨
more info is available from:
HTML::FormHandlerHTML::FormHandler::ManualHTML::FormHandler::Field
π₯ METHODS π₯
πΈ schema
stores the schema that is either passed in, created from the model name in the controller, or created from the Catalyst context and the item_class in the plugin. three origin stories, one schema π
πΈ validate_model
this is THE place to put validation that requires database-specific lookups. subclass this method in your form. validation of unique fields is called from this method. main character method energy, fr fr π
πΈ update_model
updates the database. if you wanna do some extra database processing (like updating a related table), this is the method to subclass in your form.
this routine allows the use of non-database (non-column, non-relationship) accessors in your result source class. it identifies form fields as column, relationship, select, multiple, or other. column and other fields are processed and update is called on the row. then relationships are processed. the pipeline is pipeline-ing π¦
β οΈ if the row doesn't exist (no primary key or row object was passed in), then a row is created.
πΈ lookup_options
this method is used with "Single" and "Multiple" field select lists ("single", "filter", and "multi" relationships). it returns an array reference of key/value pairs for the column passed in. the column name defined in $field->label_column will be used as the label. the default label_column is "name". the labels are sorted by Perl's cmp sort πβ¨
if there's an "active" column, then only active values are included β except if the form (item) has currently selected the inactive item. this lets existing records that reference inactive items still have those as valid select options. the inactive labels are formatted with brackets to indicate in the select list that they are inactive. considerate queen behaviour honestly π
the active column name is determined by calling:
$active_col = $form->can( 'active_column' )
? $form->active_column
: $field->active_column;
this lets you set the name of the active column globally if your tables are consistantly named (all lookup tables have the same column name to indicate they are active), or on a per-field basis. global or hyper-specific, your choice bestie π
the column to use for sorting the list is specified with "sort_column". the currently selected values in a Multiple list are grouped at the top (by the Multiple field class). top of the list = chosen ones π
πΈ init_value
this method sets a field's initial value. it is set when values are initially loaded from an item, init_object or field defaults. the origin story of a value π£
πΈ validate_unique
for fields that are marked "unique", checks the database for uniqueness. the unique constraints registered in the DBIC result source (see "add_unique_constraint" in DBIx::Class::ResultSource) will also be inspected for uniqueness unless the field's 'unique' attribute is set to false. alternatively, you can use the "unique_constraints" attribute to limit uniqueness checking to only a select group of unique constraints. error messages can be specified in the "unique_messages" attribute.
here's an example where you might want to specify a unique widget name for a given department:
has '+unique_constraints' => ( default => sub { ['department_widget_name'] } );
has '+unique_messages' => (
default => sub {
{ department_widget_name => "Please choose a unique widget name for this department" };
}
);
no duplicate widget names in this department, we simply will not allow it π π«
πΈ source
returns a DBIx::Class::ResultSource object for this Result Class. that's it, that's the whole method, short and sweet π
πΈ resultset
this method returns a resultset from the "item_class" specified in the form: "$schema->resultset( $form->item_class )" β look at the math, it's mathing β¨
π Attributes π
schemasource_nameunique_constraintsunique_messagesru_flagsDBIx::Class::ResultSet::RecursiveUpdateis used to interface withDBIx::Class. by default, the flag'unknown_params_ok'is passed in. the'ru_flags'attribute is a hashref, and it also provides'set_ru_flag'. flags for days π©
π«Ά AUTHOR π«Ά
FormHandler Contributors β see HTML::FormHandler. (yes, all of them, a whole squad, iconic π)
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2013 by Gerda Shank.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. free software, free of charge, free of drama π
and that's the whole page bestie π¦ this beauty was rendered byperl v5.38.2on2024-03-06, and the manual name on the tin isHTML::...::DBIC(3pm)β now go forth and build your forms π β¨πΈ
π¦ where this page comes from
- debian trixie: libhtml-formhandler-model-dbic-perl 0.29-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-formhandler-model-dbic-perl 0.29-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libhtml-formhandler-model-dbic-perl 0.29-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms