Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/formfu/html-formfu-model-dbic
https://github.com/formfu/html-formfu-model-dbic
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/formfu/html-formfu-model-dbic
- Owner: FormFu
- Created: 2010-11-24T20:41:31.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2020-09-04T17:21:26.000Z (about 4 years ago)
- Last Synced: 2024-06-18T14:15:41.916Z (5 months ago)
- Language: Perl
- Homepage: http://formfu.rocks
- Size: 403 KB
- Stars: 10
- Watchers: 13
- Forks: 15
- Open Issues: 3
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
Awesome Lists containing this project
README
=pod
=head1 NAME
HTML::FormFu::Model::DBIC - Integrate HTML::FormFu with DBIx::Class
=head1 VERSION
version 2.03
=head1 SYNOPSIS
Example of typical use in a Catalyst controller:
sub edit : Chained {
my ( $self, $c ) = @_;my $form = $c->stash->{form};
my $book = $c->stash->{book};if ( $form->submitted_and_valid ) {
# update dbic row with submitted values from form
$form->model->update( $book );
$c->response->redirect( $c->uri_for('view', $book->id) );
return;
}
elsif ( !$form->submitted ) {# use dbic row to set form's default values
$form->model->default_values( $book );
}return;
}=head1 SETUP
For the form object to be able to access your L schema, it needs
to be placed on the form stash, with the name C.This is easy if you're using L, as you can
set this up to happen in your Catalyst app's config file.For example, if your model is named C, you would set this
(in L format):
schema Corp
Or if your app's config file is in L format:
'Controller::HTML::FormFu':
model_stash:
schema: Corp=head1 METHODS
=head2 default_values
Arguments: $dbic_row, [\%config]
Return Value: $form
$form->model->default_values( $dbic_row );
Set a form's default values from the database, to allow a user to edit them.
=head2 update
Arguments: [$dbic_row], [\%config]
Return Value: $dbic_row
$form->model->update( $dbic_row );
Update the database with the submitted form values.
=head2 create
Arguments: [\%config]
Return Value: $dbic_row
my $dbic_row = $form->model->create( {resultset => 'Book'} );
Like L, but doesn't require a C<$dbic_row> argument.
You need to ensure the DBIC schema is available on the form stash - see
L for an example config.The C must be set either in the method arguments, or the form or
block's C.An example of setting the ResultSet name on a Form:
---
model_config:
resultset: FooTableelements:
# [snip]=head2 options_from_model
Populates a multi-valued field with values from the database.
This method should not be called directly, but is called for you during
C<< $form->process >> by fields that inherit from
L. This includes:=over
=item L
=item L
=item L
=item L
=back
To use you must set the appropriate C on the element C:
element:
- type: Select
name: foo
model_config:
resultset: TableClass=head1 BUILDING FORMS
=head2 single table
To edit the values in a row with no related rows, the field names simply have
to correspond to the database column names.For the following DBIx::Class schema:
package MySchema::Book;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("book");
__PACKAGE__->add_columns(
id => { data_type => "INTEGER" },
title => { data_type => "TEXT" },
author => { data_type => "TEXT" },
blurb => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("id");
1;
A suitable form for this might be:
elements:
- type: Text
name: title- type: Text
name: author- type: Textarea
name: blurb=head2 might_have and has_one relationships
Set field values from a related row with a C or C
relationship by placing the fields within a
L (or any element that inherits from
Block, such as L) with its
L set to the relationship name.For the following DBIx::Class schemas:
package MySchema::Book;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("book");
__PACKAGE__->add_columns(
id => { data_type => "INTEGER" },
title => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("id");
__PACKAGE__->might_have( review => 'MySchema::Review', 'book' );
1;
package MySchema::Review;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("review");
__PACKAGE__->add_columns(
id => { data_type => "INTEGER" },
book => { data_type => "INTEGER", is_nullable => 1 },
review_text => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("book");
__PACKAGE__->belongs_to( book => 'MySchema::Book' );
1;
A suitable form for this would be:
elements:
- type: Text
name: title- type: Block
nested_name: review
elements:
- type: Textarea
name: review_textFor C and C relationships, you generally shouldn't need
to have a field for the related table's primary key, as DBIx::Class will
handle retrieving the correct row automatically.You can also set a C or C relationship using a multi value
field like L.elements:
- type: Text
name: title- type: Select
nested: review
model_config:
resultset: ReviewThis will load all reviews into the select field. If you select a review from
that list, a current relationship to a review is removed and the new one is
added. This requires that the primary key of the C table and the
foreign key do not match.=head2 has_many and many_to_many relationships
The general principle is the same as for C and C above,
except you should use a L
element instead of a Block, and it needs to contain a
L field corresponding to the primary key
of the related table.The Repeatable block's
L must be set to the
name of the relationship.The Repeable block's
L
must be true (which is the default value).The Repeable block's
L must be set to
the name of a L field, which is placed
outside of the Repeatable block.
This field is used to store a count of the number of repetitions of the
Repeatable block were created.
When the form is submitted, this value is used during C<< $form->process >>
to ensure the form is rebuilt with the correct number of repetitions.To allow the user to add new related rows, either C or
C must be set - see L"Config options for Repeatable blocks">
below.For the following DBIx::Class schemas:
package MySchema::Book;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("book");
__PACKAGE__->add_columns(
id => { data_type => "INTEGER" },
title => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many( review => 'MySchema::Review', 'book' );
1;
package MySchema::Review;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("review");
__PACKAGE__->add_columns(
book => { data_type => "INTEGER" },
review_text => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("book");
__PACKAGE__->belongs_to( book => 'MySchema::Book' );
1;
A suitable form for this might be:
elements:
- type: Text
name: title- type: Hidden
name: review_count- type: Repeatable
nested_name: review
counter_name: review_count
model_config:
empty_rows: 1
elements:
- type: Hidden
name: book- type: Textarea
name: review_text=head2 belongs_to relationships
Belongs-to relationships can be edited / created with a ComboBox element.
If the user selects a value with the Select field, the belongs-to will be set
to an already-existing row in the related table.
If the user enters a value into the Text field, the belongs-to will be set
using a newly-created row in the related table.elements:
- type: ComboBox
name: author
model_config:
resultset: Author
select_column: id
text_column: nameThe element name should match the relationship name.
C<< $field->model_config->{select_column} >> should match the related primary
column.
C<< $field->model_config->{text_column} >> should match the related text
column.=head2 many_to_many selection
To select / deselect rows from a C relationship, you must use
a multi-valued element, such as a
L or a
L with
L set.The field's L must be set to the
name of the C relationship.=head3 default_column
If you want to search / associate the related table by a column other it's
primary key, set C<< $field->model_config->{default_column} >>.---
element:
- type: Checkboxgroup
name: authors
model_config:
default_column: foo=head3 link_values
If you want to set columns on the link table you can do so if you add a
C attribute to C:---
element:
- type: Checkboxgroup
name: authors
model_config:
link_values:
foo: bar=head3 additive
The default implementation will first remove all related objects and set the
new ones (see L).
If you want to add the selected objects to the current set of objects
set C in the C.---
element:
- type: Checkboxgroup
name: authors
model_config:
additive: 1
options_from_model: 0L is set to C<0> because it will try to fetch
all objects from the result class C if C is specified
without a C attribute.)=head1 COMMON ARGUMENTS
The following items are supported in the optional C hash-ref argument
to the methods L, L and L.=over
=item base
If you want the method to process a particular Block element, rather than the
whole form, you can pass the element as a C argument.$form->default_values(
$row,
{
base => $formfu_element,
},
);=item nested_base
If you want the method to process a particular Block element by
L, you can pass the name as an argument.$form->default_values(
$row,
{
nested_base => 'foo',
}'
);=back
=head1 CONFIGURATION
=head2 Config options for fields
The following items are supported as C options on form fields.
=over
=item accessor
If set, C will be used as a method-name accessor on the
C row object, instead of using the field name.=item ignore_if_empty
If the submitted value is blank, no attempt will be made to save it to the database.
=item null_if_empty
If the submitted value is blank, save it as NULL to the database. Normally an empty string is saved as NULL when its corresponding field is numeric, and as an empty string when its corresponding field is a text field. This option is useful for changing the default behavior for text fields.
=item delete_if_empty
Useful for editing a "might_have" related row containing only one field.
If the submitted value is blank, the related row is deleted.
For the following DBIx::Class schemas:
package MySchema::Book;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("book");
__PACKAGE__->add_columns(
id => { data_type => "INTEGER" },
title => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("id");
__PACKAGE__->might_have( review => 'MySchema::Review', 'book' );
1;
package MySchema::Review;
use base 'DBIx::Class';__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table("review");
__PACKAGE__->add_columns(
book => { data_type => "INTEGER" },
review_text => { data_type => "TEXT" },
);__PACKAGE__->set_primary_key("book");
__PACKAGE__->belongs_to( book => 'MySchema::Book' );
1;
A suitable form for this would be:
elements:
- type: Text
name: title- type: Block
nested_name: review
elements:
- type: Text
name: review_text
model_config:
delete_if_empty: 1=item label
To use a column value for a form field's
L.=back
=head2 Config options for fields within a Repeatable block
=over
=item delete_if_true
Intended for use on a L field.
If the checkbox is checked, the following occurs: for a has-many relationship,
the related row is deleted; for a many-to-many relationship, the relationship
link is removed.An example of use might be:
elements:
- type: Text
name: title- type: Hidden
name: review_count- type: Repeatable
nested_name: review
counter_name: review_count
elements:
- type: Hidden
name: book- type: Textarea
name: review_text- type: Checkbox
name: delete_review
label: 'Delete Review?'
model_config:
delete_if_true: 1Note: make sure the name of this field does not clash with one of your
L method names (e.g. "delete") - see L.=back
=head2 Config options for Repeatable blocks
=over
=item empty_rows
For a Repeatable block corresponding to a has-many or many-to-many
relationship, to allow the user to insert new rows, set C to
the number of extra repetitions you wish added to the end of the Repeatable
block.=item new_rows_max
Set to the maximum number of new rows that a Repeatable block is allowed to
add.If not set, it will fallback to the value of C.
=back
=head2 Config options for options_from_model
The column used for the element values is set with the C
value C - or if not set, the table's primary column is used.element:
- type: Select
name: foo
model_config:
resultset: TableClass
id_column: pk_colThe column used for the element labels is set with the C
value C - or if not set, the first text/varchar column found
in the table is used - or if one is not found, the C is used
instead.element:
- type: Select
name: foo
model_config:
resultset: TableClass
label_column: label_colTo pass the database label values via the form's localization object, set
Celement:
- type: Select
name: foo
model_config:
localize_label: 1You can set a C, which will be passed as the 1st argument to
L.element:
- type: Select
name: foo
model_config:
resultset: TableClass
condition:
type: is_fooYou can set a C, which will be passed as the 1st argument to
L.C is the column-name to be passed to
L,
and C is the name of a key on the form L
from which the value to be passed to L
is found.element:
- type: Select
name: foo
model_config:
resultset: TableClass
condition_from_stash:
key: stash_keyIs comparable to:
$form->element({
type => 'Select',
name => 'foo',
model_config => {
resultset => 'TableClass',
condition => {
key => $form->stash->{stash_key}
}
}
})If the value in the stash is nested in a data-structure, you can access it by
setting C. As you can see in the example below, it
automatically handles calling methods on objects, accessing hash-keys on
hash-references, and accessing array-slots on array references.element:
- type: Select
name: foo
model_config:
resultset: TableClass
condition_from_stash:
key: foo.bar.0
expand_stash_dots: 1Is comparable to:
$form->element({
type => 'Select',
name => 'foo',
model_config => {
resultset => 'TableClass',
condition => {
key => $form->stash->{foo}->bar->[0];
}
}
})
# Where stash returns a hashref.
# The 'foo' hash-key returns an object.
# The object-method 'bar' returns an arrayref.
# The first array slot returns the value used in the query.You can set C, which will be passed as the 2nd argument to
L.=head3 ENUM Column Type
If the field name matches (case-insensitive) a column name with type 'ENUM'
and the Schema contains enum values in
C<< $resultset->column_info($name)->{extra}{list} >>, the field's options
will be populated with the enum values.=head1 FAQ
=head2 Add extra values not in the form
To update values to the database which weren't submitted to the form,
you can first add them to the form with L.my $passwd = generate_passwd();
$form->add_valid( passwd => $passwd );
$form->model->update( $row );
C works for fieldnames that don't exist in the form.
=head2 Set a field read only
You can make a field read only. The value of such fields cannot be changed by
the user even if they submit a value for it.$field->model_config->{read_only} = 1;
- Name: field
model_config:
read_only: 1See L.
=head1 CAVEATS
To ensure your column's inflators and deflators are called, we have to
get / set values using their named methods, and not with C /
C.Because of this, beware of having column names which clash with DBIx::Class
built-in method-names, such as C. - It will have obviously
undesirable results!=head1 REMOVED METHODS
=head2 new_empty_row
See C in L"Config options for Repeatable blocks"> instead.
=head2 new_empty_row_multi
See C in L"Config options for Repeatable blocks"> instead.
=head2 Range constraint
See C in L"Config options for Repeatable blocks"> instead.
=head1 SUPPORT
Project Page:
L
Mailing list:
L
Mailing list archives:
L
=head1 BUGS
Please submit bugs / feature requests to
L (preferred) or
L.=head1 GITHUB REPOSITORY
This module's sourcecode is maintained in a git repository at
LThe project page is L
=head1 SEE ALSO
L, L, L
=head1 AUTHOR
Carl Franks
=head1 CONTRIBUTORS
Based on the code of C, which was contributed to
by:Adam Herzog
Daisuke Maki
Mario Minati
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2007 by Carl Franks
Based on the original source code of L, copyright
Thomas Klausner.This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.=cut