The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

HTML::FormHandler::Model::DBIC - Model class for FormHandler using DBIx::Class

SYNOPSIS

Subclass your form from HTML::FormHandler::Model::DBIC:

    package MyApp:Form::User;

    use Moose;
    extends 'HTML::FormHandler::Model::DBIC';

DESCRIPTION

This is a separate DBIx::Class model class for HTML::FormHandler. In addition it contains an example application (execute with t/script/bookdb_server.pl) and a form generator (HTML::FormHandler::Generator::DBIC).

It will handle normal DBIC column accessors and a number of DBIC relationships. Single Select fields will handle 'belongs_to' relationships, where the related table is used to construct a list of choices. Multiple Select fields use a many_to_many pseudo-relation to create the select list. A Compound field can represent a single relation. A Repeatable field will map onto a multiple relationship.

There are 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

The 'item_id' and '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 );

If the item_id is not defined, then a new record will be created.

The second way is to pass in a DBIx::Class row, or 'item';

   $form->process( item => $row, params => $c->req->params );

The 'item_id', 'item_class', and 'schema' will be derived from the 'item'. For a new row (such as 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 accessor are the same, but they may be different.

DESCRIPTION

This DBIC model for HTML::FormHandler will save form fields automatically to the database, will retrieve selection lists from the database (with type => 'Select' and a fieldname containing a belongs_to relationship, or type => 'Multiple' and a many_to_many relationship), and will save the selected values (one value for 'Select', multiple values in a mapping table for a 'Multiple' field).

This model supports using DBIx::Class result_source accessors just as if they were standard columns. Since the forms that use this model are subclasses of it, you can subclass any of the subroutines to provide custom functionality.

More information 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.

validate_model

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.

update_model

Updates the database. If you want to do some extra database processing (such as 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.

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 is an "active" column then only active values are included, except if the form (item) has currently selected the inactive item. This allows existing records that reference inactive items to still have those as valid select options. The inactive labels are formatted with brackets to indicate in the select list that they are inactive.

The active column name is determined by calling: $active_col = $form->can( 'active_column' ) ? $form->active_column : $field->active_column;

This allows setting 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.

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).

init_value

This method sets a field's value (for $field->value).

This method is not called if a method "init_value_$field_name" is found in the form class - that method is called instead.

validate_unique

For fields that are marked "unique", checks the database for uniqueness.

source

Returns a DBIx::Class::ResultSource object for this Result Class.

resultset

This method returns a resultset from the "item_class" specified in the form ($schema->resultset( $form->item_class ))

SUPPORT

See HTML::FormHandler

AUTHORS

Gerda Shank (gshank), gshank@cpan.org

COPYRIGHT

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