@@ -1,3 +1,10 @@
+0.05003 2009-11-25
+
+ - New 'link_values' and 'additive' options for multi-value fields for
+ many-to-many relationships.
+
+ - Doc updates.
+
0.05002 2009-07-06
- Remove prerequisite Test::MockObject - it uses UNIVERSAL::isa which
@@ -167,6 +167,8 @@ t/lib/MySchema/Manager.pm
t/lib/MySchema/Master.pm
t/lib/MySchema/Note.pm
t/lib/MySchema/Schedule.pm
+t/lib/MySchema/Task.pm
+t/lib/MySchema/TwoNote.pm
t/lib/MySchema/Type.pm
t/lib/MySchema/Type2.pm
t/lib/MySchema/User.pm
@@ -199,6 +201,7 @@ t/update/belongs_to.t
t/update/belongs_to_combobox.t
t/update/belongs_to_select.t
t/update/belongs_to_select.yml
+t/update/belongs_to_select_two.yml
t/update/column_without_field.t
t/update/column_without_field.yml
t/update/has_many_repeatable.t
@@ -248,6 +251,8 @@ t/update/nested.yml
t/update/nested_create.t
t/update/nested_create_checkbox.t
t/update/nested_name_accessor.t
+t/update/nested_repeatable_write.t
+t/update/nested_repeatable_write.yml
t/update/opt_accessor.t
t/update/opt_accessor.yml
t/update/opt_accessor_nested.t
@@ -8,7 +8,7 @@ build_requires:
configure_requires:
ExtUtils::MakeMaker: 6.42
distribution_type: module
-generated_by: 'Module::Install version 0.83'
+generated_by: 'Module::Install version 0.91'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
@@ -18,13 +18,14 @@ no_index:
directory:
- inc
- t
+ - xt
requires:
DBD::SQLite: 0
- DBIx::Class: 0.08106
+ DBIx::Class: 0.08108
HTML::FormFu: 0.05000
List::MoreUtils: 0
Task::Weaken: 0
perl: 5.8.1
resources:
license: http://dev.perl.org/licenses/
-version: 0.05002
+version: 0.05003
@@ -5,7 +5,7 @@ perl_version '5.008001';
all_from 'lib/HTML/FormFu/Model/DBIC.pm';
requires 'DBD::SQLite';
-requires 'DBIx::Class' => '0.08106';
+requires 'DBIx::Class' => '0.08108';
# DBIx::Class 0.08106 switched from DateTime::Format::MySQL to ::SQLite
# Rather than changing our prereqs depending on the installed version
# of DBIx::Class, just bump the required version
@@ -1,7 +1,11 @@
#line 1
package Module::Install::Base;
-$VERSION = '0.83';
+use strict 'vars';
+use vars qw{$VERSION};
+BEGIN {
+ $VERSION = '0.91';
+}
# Suspend handler for "redefined" warnings
BEGIN {
@@ -9,42 +13,34 @@ BEGIN {
$SIG{__WARN__} = sub { $w };
}
-### This is the ONLY module that shouldn't have strict on
-# use strict;
-
-#line 41
+#line 42
sub new {
- my ($class, %args) = @_;
-
- foreach my $method ( qw(call load) ) {
- next if defined &{"$class\::$method"};
- *{"$class\::$method"} = sub {
- shift()->_top->$method(@_);
- };
+ my $class = shift;
+ unless ( defined &{"${class}::call"} ) {
+ *{"${class}::call"} = sub { shift->_top->call(@_) };
}
-
- bless( \%args, $class );
+ unless ( defined &{"${class}::load"} ) {
+ *{"${class}::load"} = sub { shift->_top->load(@_) };
+ }
+ bless { @_ }, $class;
}
-#line 62
+#line 61
sub AUTOLOAD {
- my $self = shift;
local $@;
- my $autoload = eval {
- $self->_top->autoload
- } or return;
- goto &$autoload;
+ my $func = eval { shift->_top->autoload } or return;
+ goto &$func;
}
-#line 79
+#line 75
sub _top {
$_[0]->{_top};
}
-#line 94
+#line 90
sub admin {
$_[0]->_top->{admin}
@@ -52,7 +48,7 @@ sub admin {
Module::Install::Base::FakeAdmin->new;
}
-#line 110
+#line 106
sub is_admin {
$_[0]->admin->VERSION;
@@ -63,6 +59,7 @@ sub DESTROY {}
package Module::Install::Base::FakeAdmin;
my $fake;
+
sub new {
$fake ||= bless(\@_, $_[0]);
}
@@ -78,4 +75,4 @@ BEGIN {
1;
-#line 157
+#line 154
@@ -2,16 +2,16 @@
package Module::Install::Can;
use strict;
-use Module::Install::Base;
-use Config ();
-use File::Spec ();
-use ExtUtils::MakeMaker ();
+use Config ();
+use File::Spec ();
+use ExtUtils::MakeMaker ();
+use Module::Install::Base ();
-use vars qw{$VERSION $ISCORE @ISA};
+use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
+ $VERSION = '0.91';
+ @ISA = 'Module::Install::Base';
$ISCORE = 1;
- @ISA = qw{Module::Install::Base};
}
# check if we can load some module
@@ -2,13 +2,13 @@
package Module::Install::Fetch;
use strict;
-use Module::Install::Base;
+use Module::Install::Base ();
-use vars qw{$VERSION $ISCORE @ISA};
+use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
+ $VERSION = '0.91';
+ @ISA = 'Module::Install::Base';
$ISCORE = 1;
- @ISA = qw{Module::Install::Base};
}
sub get_file {
@@ -2,14 +2,14 @@
package Module::Install::Makefile;
use strict 'vars';
-use Module::Install::Base;
-use ExtUtils::MakeMaker ();
+use ExtUtils::MakeMaker ();
+use Module::Install::Base ();
-use vars qw{$VERSION $ISCORE @ISA};
+use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
+ $VERSION = '0.91';
+ @ISA = 'Module::Install::Base';
$ISCORE = 1;
- @ISA = qw{Module::Install::Base};
}
sub Makefile { $_[0] }
@@ -2,18 +2,17 @@
package Module::Install::Metadata;
use strict 'vars';
-use Module::Install::Base;
+use Module::Install::Base ();
use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
- @ISA = qw{Module::Install::Base};
+ $VERSION = '0.91';
+ @ISA = 'Module::Install::Base';
$ISCORE = 1;
}
my @boolean_keys = qw{
sign
- mymeta
};
my @scalar_keys = qw{
@@ -440,21 +439,21 @@ sub license_from {
/ixms ) {
my $license_text = $1;
my @phrases = (
- 'under the same (?:terms|license) as perl itself' => 'perl', 1,
- 'GNU general public license' => 'gpl', 1,
- 'GNU public license' => 'gpl', 1,
- 'GNU lesser general public license' => 'lgpl', 1,
- 'GNU lesser public license' => 'lgpl', 1,
- 'GNU library general public license' => 'lgpl', 1,
- 'GNU library public license' => 'lgpl', 1,
- 'BSD license' => 'bsd', 1,
- 'Artistic license' => 'artistic', 1,
- 'GPL' => 'gpl', 1,
- 'LGPL' => 'lgpl', 1,
- 'BSD' => 'bsd', 1,
- 'Artistic' => 'artistic', 1,
- 'MIT' => 'mit', 1,
- 'proprietary' => 'proprietary', 0,
+ 'under the same (?:terms|license) as (?:perl|the perl programming language) itself' => 'perl', 1,
+ 'GNU general public license' => 'gpl', 1,
+ 'GNU public license' => 'gpl', 1,
+ 'GNU lesser general public license' => 'lgpl', 1,
+ 'GNU lesser public license' => 'lgpl', 1,
+ 'GNU library general public license' => 'lgpl', 1,
+ 'GNU library public license' => 'lgpl', 1,
+ 'BSD license' => 'bsd', 1,
+ 'Artistic license' => 'artistic', 1,
+ 'GPL' => 'gpl', 1,
+ 'LGPL' => 'lgpl', 1,
+ 'BSD' => 'bsd', 1,
+ 'Artistic' => 'artistic', 1,
+ 'MIT' => 'mit', 1,
+ 'proprietary' => 'proprietary', 0,
);
while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
$pattern =~ s{\s+}{\\s+}g;
@@ -506,17 +505,29 @@ sub requires_from {
}
}
+sub test_requires_from {
+ my $self = shift;
+ my $content = Module::Install::_readperl($_[0]);
+ my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
+ while ( @requires ) {
+ my $module = shift @requires;
+ my $version = shift @requires;
+ $self->test_requires( $module => $version );
+ }
+}
+
# Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
# numbers (eg, 5.006001 or 5.008009).
# Also, convert double-part versions (eg, 5.8)
sub _perl_version {
my $v = $_[-1];
- $v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
+ $v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
$v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
$v =~ s/(\.\d\d\d)000$/$1/;
$v =~ s/_.+$//;
if ( ref($v) ) {
- $v = $v + 0; # Numify
+ # Numify
+ $v = $v + 0;
}
return $v;
}
@@ -526,23 +537,58 @@ sub _perl_version {
######################################################################
-# MYMETA.yml Support
+# MYMETA Support
sub WriteMyMeta {
die "WriteMyMeta has been deprecated";
}
-sub write_mymeta {
+sub write_mymeta_yaml {
my $self = shift;
-
- # If there's no existing META.yml there is nothing we can do
- return unless -f 'META.yml';
# We need YAML::Tiny to write the MYMETA.yml file
unless ( eval { require YAML::Tiny; 1; } ) {
return 1;
}
+ # Generate the data
+ my $meta = $self->_write_mymeta_data or return 1;
+
+ # Save as the MYMETA.yml file
+ print "Writing MYMETA.yml\n";
+ YAML::Tiny::DumpFile('MYMETA.yml', $meta);
+}
+
+sub write_mymeta_json {
+ my $self = shift;
+
+ # We need JSON to write the MYMETA.json file
+ unless ( eval { require JSON; 1; } ) {
+ return 1;
+ }
+
+ # Generate the data
+ my $meta = $self->_write_mymeta_data or return 1;
+
+ # Save as the MYMETA.yml file
+ print "Writing MYMETA.json\n";
+ Module::Install::_write(
+ 'MYMETA.json',
+ JSON->new->pretty(1)->canonical->encode($meta),
+ );
+}
+
+sub _write_mymeta_data {
+ my $self = shift;
+
+ # If there's no existing META.yml there is nothing we can do
+ return undef unless -f 'META.yml';
+
+ # We need Parse::CPAN::Meta to load the file
+ unless ( eval { require Parse::CPAN::Meta; 1; } ) {
+ return undef;
+ }
+
# Merge the perl version into the dependencies
my $val = $self->Meta->{values};
my $perl = delete $val->{perl_version};
@@ -558,7 +604,7 @@ sub write_mymeta {
}
# Load the advisory META.yml file
- my @yaml = YAML::Tiny::LoadFile('META.yml');
+ my @yaml = Parse::CPAN::Meta::LoadFile('META.yml');
my $meta = $yaml[0];
# Overwrite the non-configure dependency hashs
@@ -572,9 +618,7 @@ sub write_mymeta {
$meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };
}
- # Save as the MYMETA.yml file
- print "Writing MYMETA.yml\n";
- YAML::Tiny::DumpFile('MYMETA.yml', $meta);
+ return $meta;
}
1;
@@ -2,12 +2,12 @@
package Module::Install::Win32;
use strict;
-use Module::Install::Base;
+use Module::Install::Base ();
use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
- @ISA = qw{Module::Install::Base};
+ $VERSION = '0.91';
+ @ISA = 'Module::Install::Base';
$ISCORE = 1;
}
@@ -2,11 +2,11 @@
package Module::Install::WriteAll;
use strict;
-use Module::Install::Base;
+use Module::Install::Base ();
use vars qw{$VERSION @ISA $ISCORE};
BEGIN {
- $VERSION = '0.83';
+ $VERSION = '0.91';;
@ISA = qw{Module::Install::Base};
$ISCORE = 1;
}
@@ -41,8 +41,18 @@ sub WriteAll {
# The Makefile write process adds a couple of dependencies,
# so write the META.yml files after the Makefile.
- $self->Meta->write if $args{meta};
- $self->Meta->write_mymeta if $self->mymeta;
+ if ( $args{meta} ) {
+ $self->Meta->write;
+ }
+
+ # Experimental support for MYMETA
+ if ( $ENV{X_MYMETA} ) {
+ if ( $ENV{X_MYMETA} eq 'JSON' ) {
+ $self->Meta->write_mymeta_json;
+ } else {
+ $self->Meta->write_mymeta_yaml;
+ }
+ }
return 1;
}
@@ -28,7 +28,7 @@ BEGIN {
# This is not enforced yet, but will be some time in the next few
# releases once we can make sure it won't clash with custom
# Module::Install extensions.
- $VERSION = '0.83';
+ $VERSION = '0.91';
# Storage for the pseudo-singleton
$MAIN = undef;
@@ -353,7 +353,7 @@ sub _read {
if ( $] >= 5.006 ) {
open( FH, '<', $_[0] ) or die "open($_[0]): $!";
} else {
- open( FH, "< $_[0]" ) or die "open($_[0]): $!";
+ open( FH, "< $_[0]" ) or die "open($_[0]): $!";
}
my $string = do { local $/; <FH> };
close FH or die "close($_[0]): $!";
@@ -384,7 +384,7 @@ sub _write {
if ( $] >= 5.006 ) {
open( FH, '>', $_[0] ) or die "open($_[0]): $!";
} else {
- open( FH, "> $_[0]" ) or die "open($_[0]): $!";
+ open( FH, "> $_[0]" ) or die "open($_[0]): $!";
}
foreach ( 1 .. $#_ ) {
print FH $_[$_] or die "print($_[0]): $!";
@@ -9,7 +9,7 @@ use Scalar::Util qw( blessed );
use Storable qw( dclone );
use Carp qw( croak );
-our $VERSION = '0.05002';
+our $VERSION = '0.05003';
$VERSION = eval $VERSION;
# sub _compatible_config() is only required as long as we support deprecated
@@ -655,46 +655,46 @@ sub _insert_has_many {
sub _can_insert_new_row {
my ( $dbic, $form, $config, $repetition, $rel, $pk_field ) = @_;
-
+
if ( $config->{new_empty_row} ) {
# old, deprecated behaviour
-
+
my $rows = $config->{new_empty_row};
-
+
$rows = [$rows] if ref $rows ne 'ARRAY';
-
+
for my $name (@$rows) {
my ($field)
= grep { $_->original_name eq $name } @{ $repetition->get_fields };
-
+
return if !defined $field;
-
+
my $nested_name = $field->nested_name;
return if !$form->valid($nested_name);
-
+
my $value = $form->param_value($nested_name);
return if !length $value;
}
}
else {
# new behaviour
-
+
my @rep_fields = @{ $repetition->get_fields };
-
+
my $pk_name = $pk_field->nested_name;
-
+
my @constraints = grep { $_->when->{field} eq $pk_name }
grep { defined $_->when }
map { @{ $_->get_constraints({ type => 'Required' }) } }
@rep_fields;
-
+
my @required_fields;
-
+
if ( @constraints ) {
# if there are any Required constraints whose 'when' clause points to
# the PK field - check that all these fields are filled in - as
# the PK value is missing on new reps, so the constraint won't have run
-
+
return if
notall { defined && length }
map { $form->param_value( $_->nested_name ) }
@@ -704,11 +704,11 @@ sub _can_insert_new_row {
else {
# otherwise, just check at least 1 field that matches either a column
# name or an accessor, is filled in
-
+
my $result_source = $dbic->$rel->result_source;
-
+
# only create a new record if (read from bottom)...
-
+
return if
none { defined && length }
map { $form->param_value( $_->nested_name ) }
@@ -720,7 +720,7 @@ sub _can_insert_new_row {
@rep_fields;
}
}
-
+
return 1;
}
@@ -746,25 +746,25 @@ sub _delete_has_many {
sub _fix_value {
my ( $dbic, $col, $value, $field, ) = @_;
-
+
my $col_info = $dbic->column_info($col);
my $is_nullable = $col_info->{is_nullable} || 0;
my $data_type = $col_info->{data_type} || '';
-
+
if ( defined $value ) {
if ( ( $is_nullable
|| $data_type =~ m/^timestamp|date|int|float|numeric/i
)
# comparing to '' does not work for inflated objects
- && !ref $value
+ && !ref $value
&& $value eq ''
)
{
$value = undef;
}
}
-
+
if ( !defined $value
&& defined $field
&& $field->isa('HTML::FormFu::Element::Checkbox')
@@ -772,7 +772,7 @@ sub _fix_value {
{
$value = 0;
}
-
+
return $value;
}
@@ -781,19 +781,19 @@ sub _save_columns {
for my $field ( @{ $base->get_fields }, ) {
next if not is_direct_child( $base, $field );
-
+
my $config = _compatible_config($field);
next if $config->{delete_if_true};
next if $config->{read_only};
-
+
my $name = $field->name;
$name = $field->original_name if $field->original_name;
-
+
my $accessor = $config->{accessor} || $name;
next if not defined $accessor;
-
+
my $value = $form->param_value( $field->nested_name );
-
+
next if $config->{ignore_if_empty} && ( !defined $value || $value eq "" );
my ($pk) = $dbic->result_source->primary_columns;
@@ -825,7 +825,7 @@ sub _save_columns {
{
$dbic->set_column( $accessor, $value );
}
- elsif ( $dbic->can($accessor)
+ elsif ( $dbic->can($accessor)
# and $accessor is not a has_one or might_have rel where the foreign key is on the foreign table
and !$dbic->result_source->relationship_info($accessor)) {
$dbic->$accessor($value);
@@ -847,17 +847,17 @@ sub _save_columns {
for my $valid ( $form->valid ) {
next if @{ $base->get_fields( name => $valid ) };
next if not $dbic->can($valid);
-
+
my $value = $form->param_value($valid);
$dbic->$valid($value);
}
-
+
return 1;
}
sub _save_multi_value_fields_many_to_many {
my ( $base, $dbic, $form, $attrs, $rels, $cols ) = @_;
-
+
my @fields = grep {
( defined $attrs->{nested_base} && defined $_->parent->nested_name )
? $_->parent->nested_name eq $attrs->{nested_base}
@@ -883,12 +883,13 @@ sub _save_multi_value_fields_many_to_many {
my @values = $form->param_list($nested_name);
my @rows;
- if (@values) {
- my $config = _compatible_config($field);
+ my $config = _compatible_config($field);
- my ($pk) = $config->{default_column}
- || $related->result_source->primary_columns;
+ my ($pk) = $config->{default_column}
+ || $related->result_source->primary_columns;
+ if (@values) {
+
$pk = "me.$pk" unless $pk =~ /\./;
@rows = $related->result_source->resultset->search( {
@@ -896,9 +897,24 @@ sub _save_multi_value_fields_many_to_many {
$pk => { -in => \@values } } )->all;
}
- my $set_method = "set_$name";
-
- $dbic->$set_method( \@rows );
+ if($config->{additive}) {
+
+ my $relinfo = $dbic->result_source->relationship_info($name);
+
+ $pk =~ s/^.*\.//;
+
+ my $set_method = "add_to_$name";
+ my $remove_method = "remove_from_$name";
+
+ foreach my $row ( @rows ) {
+ $dbic->$remove_method($row);
+ $dbic->$set_method($row, $config->{link_values});
+ }
+ } else {
+ my $set_method = "set_$name";
+
+ $dbic->$set_method( \@rows, $config->{link_values} );
+ }
}
}
}
@@ -1050,26 +1066,26 @@ 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;
}
@@ -1139,7 +1155,7 @@ An example of setting the ResultSet name on a Form:
---
model_config:
resultset: FooTable
-
+
elements:
# [snip]
@@ -1182,20 +1198,20 @@ 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:
@@ -1203,18 +1219,18 @@ 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<might_have> or C<has_one>
-relationship by placing the fields within a
-L<Block|HTML::FormFu::Element::Block> (or any element that inherits from
+Set field values from a related row with a C<might_have> or C<has_one>
+relationship by placing the fields within a
+L<Block|HTML::FormFu::Element::Block> (or any element that inherits from
Block, such as L<Fieldset|HTML::FormFu::Element::Fieldset>) with its
L<HTML::FormFu/nested_name> set to the relationship name.
@@ -1222,40 +1238,40 @@ 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:
@@ -1263,7 +1279,7 @@ A suitable form for this would be:
elements:
- type: Text
name: title
-
+
- type: Block
nested_name: review
elements:
@@ -1275,20 +1291,20 @@ 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<has_one> or C<might_have> relationship using a multi value
-field like L<Select|HTML::FormFu::Element::Select>.
+field like L<Select|HTML::FormFu::Element::Select>.
elements:
- type: Text
name: title
-
+
- type: Select
nested: review
model_config:
resultset: Review
This 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<Review> table and the
+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<Review> table and the
foreign key do not match.
=head2 has_many and many_to_many relationships
@@ -1313,45 +1329,45 @@ 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 rebuild with the correct number of repetitions.
+to ensure the form is rebuilt with the correct number of repetitions.
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 would be:
@@ -1359,29 +1375,29 @@ A suitable form for this would 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
=head2 many_to_many selection
To select / deselect rows from a C<many_to_many> relationship, you must use
-a multi-valued element, such as a
+a multi-valued element, such as a
L<Checkboxgroup|HTML::FormFu::Element::Checkboxgroup> or a
-L<Select|HTML::FormFu::Element::Select> with
+L<Select|HTML::FormFu::Element::Select> with
L<multiple|HTML::FormFu::Element::Select/multiple> set.
-The field's L<name|HTML::FormFu::Element::_Field/name> must be set to the
+The field's L<name|HTML::FormFu::Element::_Field/name> must be set to the
name of the C<many_to_many> relationship.
=item default_column
@@ -1396,6 +1412,34 @@ primary key, set C<< $field->model_config->{default_column} >>.
model_config:
default_column: foo
+If you want to set columns on the link table you can do so if you add a
+C<link_values> attribute to C<model_config>:
+
+ ---
+ element:
+ - type: Checkboxgroup
+ name: authors
+ model_config:
+ link_values:
+ foo: bar
+
+
+The default implementation will first remove all related objects and set the
+new ones (see L<http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#set_$rel>).
+If you want to add the selected objects to the current set of objects
+set C<additive> in the C<model_config>.
+
+ ---
+ element:
+ - type: Checkboxgroup
+ name: authors
+ model_config:
+ additive: 1
+ options_from_model: 0
+
+(<options_from_model> is set to C<0> because this L</options_from_model> will try to fetch
+all objects from the result class C<Authors> if C<model_config> is specified
+without a C<resultset> attribute.)
=head1 COMMON ARGUMENTS
@@ -1453,39 +1497,39 @@ 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:
@@ -1493,7 +1537,7 @@ A suitable form for this would be:
elements:
- type: Text
name: title
-
+
- type: Block
nested_name: review
elements:
@@ -1526,20 +1570,20 @@ 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?'
@@ -1564,6 +1608,11 @@ 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<empty_rows>.
+
=back
=head2 Config options for options_from_model
@@ -1617,13 +1666,13 @@ L<DBIx::Class::ResultSet/search>.
=head2 Add extra values not in the form
-To update values to the database which weren't submitted to 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<add_valid|HTML::FormFu/add_valid>.
my $passwd = generate_passwd();
-
+
$form->add_valid( passwd => $passwd );
-
+
$form->model->update( $row );
C<add_valid> works for fieldnames that don't exist in the form.
@@ -1634,7 +1683,7 @@ 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: 1
@@ -1655,7 +1704,7 @@ See C<empty_rows> in L</"Config options for Repeatable blocks"> instead.
Is deprecated and provided only for backwards compatability.
Will be removed at some point in the future.
-See C<empty_rows> in L</"Config options for Repeatable blocks"> instead.
+See C<new_rows_max> in L</"Config options for Repeatable blocks"> instead.
=head2 Range constraint
@@ -1669,12 +1718,12 @@ See C<empty_rows> in L</"Config options for Repeatable blocks"> instead.
=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<get_column> /
+To ensure your column's inflators and deflators are called, we have to
+get / set values using their named methods, and not with C<get_column> /
C<set_column>.
-Because of this, beware of having column names which clash with DBIx::Class
-built-in method-names, such as C<delete>. - It will have obviously
+Because of this, beware of having column names which clash with DBIx::Class
+built-in method-names, such as C<delete>. - It will have obviously
undesirable results!
=head1 SUPPORT
@@ -1693,23 +1742,23 @@ L<http://lists.scsys.co.uk/pipermail/html-formfu/>
=head1 BUGS
-Please submit bugs / feature requests to
-L<http://code.google.com/p/html-formfu/issues/list> (preferred) or
+Please submit bugs / feature requests to
+L<http://code.google.com/p/html-formfu/issues/list> (preferred) or
L<http://rt.perl.org>.
=head1 SUBVERSION REPOSITORY
-The publicly viewable subversion code repository is at
+The publicly viewable subversion code repository is at
L<http://html-formfu.googlecode.com/svn/trunk/HTML-FormFu-Model-DBIC>.
-If you wish to contribute, you'll need a GMAIL email address. Then just
+If you wish to contribute, you'll need a GMAIL email address. Then just
ask on the mailing list for commit access.
-If you wish to contribute but for some reason really don't want to sign up
-for a GMAIL account, please post patches to the mailing list (although
-you'll have to wait for someone to commit them).
+If you wish to contribute but for some reason really don't want to sign up
+for a GMAIL account, please post patches to the mailing list (although
+you'll have to wait for someone to commit them).
-If you have commit permissions, use the HTTPS repository url:
+If you have commit permissions, use the HTTPS repository url:
L<https://html-formfu.googlecode.com/svn/trunk/HTML-FormFu-Model-DBIC>
=head1 SEE ALSO
@@ -1735,7 +1784,7 @@ Mario Minati
Copyright (C) 2007 by Carl Franks
-Based on the original source code of L<DBIx::Class::HTMLWidget>, copyright
+Based on the original source code of L<DBIx::Class::HTMLWidget>, copyright
Thomas Klausner.
This library is free software; you can redistribute it and/or modify
@@ -73,6 +73,16 @@ SQL
$dbh->do( <<SQL );
+CREATE TABLE two_note (
+ id INTEGER NOT NULL,
+ two_note_id INTEGER PRIMARY KEY NOT NULL,
+ note TEXT NOT NULL
+);
+
+SQL
+
+
+ $dbh->do( <<SQL );
CREATE TABLE user (
id INTEGER PRIMARY KEY NOT NULL,
master INTEGER,
@@ -97,6 +107,7 @@ SQL
CREATE TABLE user_band (
user INTEGER NOT NULL,
band INTEGER NOT NULL,
+ rating INTEGER,
PRIMARY KEY (user, band)
);
@@ -166,6 +177,17 @@ CREATE TABLE manager (
SQL
+ $dbh->do( <<SQL );
+CREATE TABLE task (
+ id INTEGER PRIMARY KEY NOT NULL,
+ schedule INTEGER NOT NULL,
+ deadline DATETIME,
+ detail TEXT NOT NULL
+);
+
+SQL
+
+
}
@@ -19,5 +19,7 @@ __PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to( master => 'MySchema::Master' );
+__PACKAGE__->has_many( tasks => 'MySchema::Task', 'schedule' );
+
1;
@@ -0,0 +1,23 @@
+package MySchema::Task;
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw/ Core /);
+
+__PACKAGE__->table("task");
+
+__PACKAGE__->add_columns(
+ id => { data_type => "INTEGER", is_nullable => 0 },
+ schedule => { data_type => "INTEGER", is_nullable => 0 },
+ deadline => { data_type => "DATETIME", is_nullable => 0 },
+ detail => { data_type => "TEXT", is_nullable => 0 },
+);
+
+__PACKAGE__->set_primary_key("id");
+
+__PACKAGE__->belongs_to( schedule => 'MySchema::Schedule' );
+
+1;
+
@@ -0,0 +1,22 @@
+package MySchema::TwoNote;
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw/ Core /);
+
+__PACKAGE__->table("two_note");
+
+__PACKAGE__->add_columns(
+ id => { data_type => "INTEGER", is_nullable => 0 },
+ two_note_id => { data_type => "INTEGER", is_nullable => 0 },
+ note => { data_type => "TEXT", is_nullable => 0 },
+);
+
+__PACKAGE__->set_primary_key("two_note_id");
+
+__PACKAGE__->belongs_to( id => 'MySchema::Master', { id => 'id' } );
+
+1;
+
@@ -17,6 +17,10 @@ __PACKAGE__->add_columns(
data_type => "INTEGER",
is_nullable => 0,
},
+ rating => {
+ data_type => "INTEGER",
+ is_nullable => 1,
+ }
);
__PACKAGE__->set_primary_key( "user", "band" );
@@ -1,6 +1,6 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More tests => 4;
use HTML::FormFu;
use lib 't/lib';
@@ -33,3 +33,26 @@ my $note = $schema->resultset("Note")->find(1);
is($note->master->id, 2);
is($note->note, 'foo');
+
+$form->load_config_file('t/update/belongs_to_select_two.yml');
+
+$schema = MySchema->connect('dbi:SQLite:dbname=t/test.db');
+
+$form->stash->{schema} = $schema;
+
+$rs = $schema->resultset('Master');
+
+$rs->create( { id => 4 } );
+$rs->create( { id => 5 } );
+
+$master = $rs->create( { text_col => 'b', type_id => 2, type2_id => 2 } );
+
+$form->process( { id => 4, note => 'foo' } );
+
+$form->model->create;
+
+$note = $schema->resultset("TwoNote")->find(1);
+
+is($note->id->id, 4);
+
+is($note->note, 'foo');
@@ -0,0 +1,8 @@
+---
+ model_config:
+ resultset: TwoNote
+ elements:
+ - name: two_note_id
+ - name: note
+ - name: id
+ type: Select
\ No newline at end of file
@@ -0,0 +1,78 @@
+use strict;
+use warnings;
+use Test::More tests => 7;
+
+use HTML::FormFu;
+use lib 't/lib';
+use DBICTestLib 'new_db';
+use MySchema;
+
+new_db();
+
+my $form = HTML::FormFu->new;
+
+$form->load_config_file('t/update/nested_repeatable_write.yml');
+
+my $schema = MySchema->connect('dbi:SQLite:dbname=t/test.db');
+
+my $master = $schema->resultset('Master')->create({ id => 1 });
+
+# first sub-record
+{
+ # schedule 1
+ my $u1 = $master->create_related( 'schedules', { note => 'some appointment',
+ date => '02-02-2009' } );
+
+ # task 1
+ $u1->create_related( 'tasks' => { detail => 'associated to do item' } );
+}
+
+# second sub-record
+{
+ # schedule 2
+ my $u2 = $master->create_related( 'schedules', { note => 'some other appointment',
+ date => '03-03-2009' } );
+
+ # task 2
+ $u2->create_related( 'tasks', { detail => 'action item 1' } );
+
+ # task 3
+ $u2->create_related( 'tasks', { detail => 'action item 2' } );
+}
+
+{
+ $form->process( {
+ 'sched_count' => 2,
+ 'schedules_2.id' => 2,
+ 'schedules_2.note' => 'new appointment',
+ 'schedules_2.count' => 2,
+ 'schedules_2.tasks_1.id' => 2,
+ 'schedules_2.tasks_1.detail' => 'new action item 1',
+ 'schedules_2.tasks_2.id' => 3,
+ 'schedules_2.tasks_2.detail' => 'new action item 2',
+ } );
+
+ ok( $form->submitted_and_valid );
+
+ my $row = $schema->resultset('Master')->find(1);
+
+ $form->model->update($row);
+}
+
+{
+ my $schedule = $schema->resultset('Schedule')->find(2);
+
+ is( $schedule->note, 'new appointment' );
+
+ my @add = $schedule->tasks->all;
+
+ is( scalar @add, 2 );
+
+ is( $add[0]->id, 2 );
+ is( $add[0]->detail, 'new action item 1' );
+
+ is( $add[1]->id, 3 );
+ is( $add[1]->detail, 'new action item 2' );
+
+}
+
@@ -0,0 +1,38 @@
+---
+auto_fieldset: 1
+
+elements:
+- type: Hidden
+ name: id
+
+- type: Hidden
+ name: sched_count
+
+- type: Repeatable
+ nested_name: schedules
+ counter_name: sched_count
+ elements:
+
+ - type: Hidden
+ name: id
+
+ - type: Text
+ name: note
+
+ - type: Hidden
+ name: count
+
+ - type: Repeatable
+ nested_name: tasks
+ counter_name: count
+
+ elements:
+ - type: Hidden
+ name: id
+
+ - type: Text
+ name: detail
+
+- type: Submit
+ name: submit
+