Subject: | add_valid with nested fields.. |
When having nested fieldsets, using add_valid to set values on elements
of the nested fieldset does not result in those values being saved to
the db.
Example:
Fieldset
nested_name: foo
elements:
Fieldset
nested_name: bar
elements:
$form->add_valid('foo.bar.myVal', 'thing');
foo.bar will be created in the db, but myVal will not be set.
Attached is a patch which adds the functionality, but I am not sure if
what I did is really the best way to handle it.
Subject: | formfu_add_valid.patch |
--- lib/perl5/HTML/FormFu/Model/DBIC.pm 2009-10-19 10:31:08.000000000 -0500
+++ lib/HTML/FormFu/Model/DBIC.pm 2009-10-19 10:57:07.000000000 -0500
@@ -837,6 +855,27 @@
}
}
+ ### Begin Patch for setting values, on nested results, which were set with add_valid.
+ if (defined($base->nested_name)) {
+ my $proper_name = _return_proper_name($base);
+ for my $valid ( $form->valid ) {
+ # if the nested name is in the field name, take it out..
+ my $acs;
+ my $check_for_name = $proper_name;
+ if ($valid =~ /$check_for_name\./) {
+ $acs = $valid;
+ $acs =~ s/$check_for_name\.//;
+ next if not $dbic->can($acs);
+ } else {
+ next;
+ }
+ next if @{ $form->get_fields( name => $acs ) };
+ my $value = $form->param_value($valid);
+ $dbic->$acs($value);
+ }
+ }
+ ### End Patch for nested add_valid
+
# for values inserted by add_valid - and not correlated to any field in the form
my $parent = $base;
do {
@@ -855,6 +894,28 @@
return 1;
}
+### Patch for setting values on nested results added with add_valid.
+### The above section of patch, required the full field name, i.e. my_dbic_rel.my_field or my_dbic_rel.other_rel.field
+### This code returns that field name, starting from a base element (my_field, field)
+sub _return_proper_name {
+ my ($base) = @_;
+ my $name = $base->nested_name;
+ my $test_base = $base;
+ do {
+ $test_base = $test_base->parent;
+ if (defined($test_base)) {
+ if (($test_base->isa('HTML::FormFu::Element::Block') || $test_base->isa('HTML::FormFu::Element::Fieldset')) && !$test_base->isa('HTML::FormFu::Element::Repeatable')) {
+ my $add_to = $test_base->nested_name;
+ if (defined($add_to) && $add_to ne '') {
+ $name = $add_to . '.' . $name;
+ }
+ }
+ }
+ } until (!defined($test_base));
+ return $name;
+}
+### End patch for setting values on nested results added with add_valid.
+
sub _save_multi_value_fields_many_to_many {
my ( $base, $dbic, $form, $attrs, $rels, $cols ) = @_;