[html-formfu] Model/DBIC.pm, new_empty_row_amount, minimum range, doc improvements

Oskari 'Okko' Ojala okko+formfu at frantic.com
Thu Jan 22 10:05:55 GMT 2009


Hello Formfuers,

please find attached a patch to 
HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm. It is against revision 
1293, the most recent in svn. I would appreciate if someone could commit 
it to the svn.

It contains the following improvements:

-It is possible to specify "new_empty_row_amount" to produce more than 
one empty row in the initial form. I need it, and it has been asked on the 
list earlier.

-It is possible to specify minimum in the Repeatable's count field's Range 
constraint, complementing the earlier implementation of maximum.

Both changes are backwards compatible.


It contains the following documentation improvements:

-Documentation of the above two features.

-Mentioning that "The repeatable block must have a Hidden field for the 
Primary Key." I personally fought with this for quite some time before I 
found it out in the source, as I was debugging with the PK field as "Text" 
so that I could see it, but just couldn't get it to work.

-Explicit naming of the counter with "counter_name" in the example, so 
that it is easier to see how to implement multiple Repeatables in the same 
form.


Regards,

 	Okko

-- 
Oskari "Okko" Ojala
-------------- next part --------------
--- ./HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm	2009-01-22 11:14:26.000000000 +0200
+++ ./HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm	2009-01-22 11:28:13.000000000 +0200
@@ -276,12 +276,22 @@
                     ( defined $_->original_name ? $_->original_name : $_->name )
                 } @{ $block->get_fields( { type => 'Hidden' } ) };
 
+            my $minimum_count;
+            if (defined( my $param_name = $block->counter_name) ) {
+                my $field = $base->get_field($param_name);
+                if (defined($field) && defined (my $range =  $field->get_constraint({type => 'Range'}))) {
+                    $minimum_count = $range->{'minimum'} || 0;
+                }
+            }
+
             my @rows = $dbic->$rel->all;
             my $count
                 = $config->{new_empty_row}
-                ? scalar @rows + 1
+	         ? scalar @rows + ($config->{new_empty_row_amount} || 1)
                 : scalar @rows;
 
+            $count = $minimum_count if (defined($minimum_count) && $count<$minimum_count);
+
             my $blocks = $block->repeat($count);
 
             for my $rep ( 0 .. $#rows ) {
@@ -996,7 +1006,8 @@
 L<Repeatable|HTML::FormFu::Element::Repeatable> element.
 This will output a repetition of the entire block for each row returned.
 L<HTML::FormFu::Element::Repeatable/increment_field_names> must be true
-(which is the default value).
+(which is the default value). The repeatable block must have a
+L<HTML::FormFu::Element::Hidden> field for the Primary Key.
 
 The block's L<nested_name|HTML::FormFu::Element::Repeatable/nested_name>
 must be set to the name of the relationship.
@@ -1023,14 +1034,21 @@
 
 If you want to add more than one new row you can use
 C<< $block->model_config->{new_empty_row_multi} >> instead of
-C<< $block->model_config->{new_empty_row} >>. To limit the maximum number of new 
+C<< $block->model_config->{new_empty_row} >>. The amount of new empty
+rows can be specified with
+C<< $block->model_config->{new_empty_row_amount} >>, default is 1.
+
+To limit the maximum number of new 
 rows put a L<range|HTML::FormFu::Constraint::Range> constraint on the
-C<count> field.
+C<count> field. To limit the minimum number of rows, define the minimum
+amount in the same L<range|HTML::FormFu::Constraint::Range> constraint.
+FormFu will then append new empty rows to reach the minimum amount.
 
     ---
     element:
       - type: Repeatable
         nested_name: authors
+        counter_name: authors_count
         model_config:
           new_empty_row_multi: author
         
@@ -1042,7 +1060,7 @@
             name: author
             
       - type: Hidden
-        name: count
+        name: authors_count
         constraints: 
           - type: Range
             max: 3


More information about the HTML-FormFu mailing list