HTML::...::DBIC(3pm)

πŸ“¦
version 0.29 Β· 3 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– 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::FormHandler
  • HTML::FormHandler::Manual
  • HTML::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 πŸŽ€

  • schema
  • source_name
  • unique_constraints
  • unique_messages
  • ru_flags
    • DBIx::Class::ResultSet::RecursiveUpdate is used to interface with DBIx::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 πŸ‘‘)


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 by perl v5.38.2 on 2024-03-06, and the manual name on the tin is HTML::...::DBIC(3pm) β€” now go forth and build your forms πŸ’…βœ¨πŸŒΈ
πŸ“¦ where this page comes from