Browse files

compound fields with 'not_nullable' now returns hashref with empty su…

…bfields instead of undef
  • Loading branch information...
1 parent 211459a commit b811bc6237774d1b10a27e0d7c8083c5ef510c73 @gshank committed Jun 25, 2012
View
6 Changes
@@ -1,8 +1,14 @@
+0.40013 Sun Jun 24, 2012
Re-write elimination of PrimaryKey field from repeatable value,
add 'no_value_if_empty' attribute
Fix bug with labels '0' not being displayed
Change 'missing' to an attribute.
Submit field has 'submit' html5_type_attr
+ Add html5_type_attr to Hidden, Reset, and Password fields
+ Add new behavior for compound fields and 'not_nullable' flag,
+ where compound field value is not set to undef when all
+ subfields are empty. This is needed for some kinds of db
+ relationships, to ensure that subfields are set to null.
0.40012 Fri Jun 15, 2012
Bug cloning merging repeatable instances; form reference garbage collected
View
3 lib/HTML/FormHandler/Field.pm
@@ -443,6 +443,9 @@ an empty string instead of a null value (such as a NOT NULL column), set this at
This attribute is also used when you want an empty array to stay an empty array and not
be set to undef.
+It's also used when you have a compound field and you want the 'value' returned
+to contain subfields with undef, instead of the whole field to be undef.
+
=back
=head2 apply
View
7 lib/HTML/FormHandler/Manual/Database.pod
@@ -75,6 +75,13 @@ PrimaryKey field, or add extra values in update_model.
See also L<HTML::FormHandler::Field::Compound>.
+The default for compound fields is that if all subfields are empty,
+the value of the compound field is set to undef (null). For some types
+of relations, you may want to set the 'not_nullable' flag to force
+the field to contain all subfields anyway, such as when the related
+rows are not deleted when empty. See test t/compound/empty.t for a
+demonstration of the difference in output.
+
=head2 Repeatable fields
The 'Repeatable' field type allows you to update arrays of columns from related tables
View
5 lib/HTML/FormHandler/Validate.pm
@@ -78,13 +78,16 @@ sub validate_field {
elsif ( !$field->input_defined ) {
if ( $field->not_nullable ) {
$field->_set_value($field->input);
+ # handles the case where a compound field value needs to have empty subfields
+ $continue_validation = 0 unless $field->has_flag('is_compound');
}
elsif ( $field->no_value_if_empty || $field->has_flag('is_contains') ) {
+ $continue_validation = 0;
}
else {
$field->_set_value(undef);
+ $continue_validation = 0;
}
- $continue_validation = 0;
}
return if ( !$continue_validation && !$field->validate_when_empty );
View
57 t/compound/default.t
@@ -0,0 +1,57 @@
+use strict;
+use warnings;
+use Test::More;
+
+# tests that a 'default' hashref on a compound field works
+{
+ {
+ package MyApp::Test::Compound;
+ use HTML::FormHandler::Moose;
+ extends 'HTML::FormHandler';
+
+ has_field 'comp_foo' => ( type => 'Compound', default => { one => 1, two => 2, three => 3 } );
+ has_field 'comp_foo.one';
+ has_field 'comp_foo.two';
+ has_field 'comp_foo.three';
+ }
+
+ my $form = MyApp::Test::Compound->new;
+ ok( $form );
+ $form->process;
+ is( $form->field('comp_foo.one')->value, 1, 'value is one' );
+ is_deeply( $form->field('comp_foo')->value, { one => 1, two => 2, three => 3 },
+ 'value for compound is correct' );
+}
+
+
+# tests that default object for a compound field works
+# object provided by default_method
+{
+ {
+ package MyApp::Foo;
+ use Moose;
+ has 'one' => ( is => 'ro', default => 1 );
+ has 'two' => ( is => 'ro', default => 2 );
+ has 'three' => ( is => 'ro', default => 3 );
+ }
+ {
+ package MyApp::Test::Compound2;
+ use HTML::FormHandler::Moose;
+ extends 'HTML::FormHandler';
+
+ has_field 'comp_foo' => ( type => 'Compound', default_method => \&default_comp_foo );
+ has_field 'comp_foo.one';
+ has_field 'comp_foo.two';
+ has_field 'comp_foo.three';
+ sub default_comp_foo {
+ return MyApp::Foo->new;
+ }
+ }
+
+ my $form = MyApp::Test::Compound2->new;
+ ok( $form );
+ is( $form->field('comp_foo.one')->value, 1, 'value is one' );
+ is( ref $form->field('comp_foo')->item, 'MyApp::Foo', 'item saved' );
+}
+
+done_testing;

0 comments on commit b811bc6

Please sign in to comment.