Skip Menu |

This queue is for tickets about the HTML-FormFu-Model-DBIC CPAN distribution.

Report information
The Basics
Id: 53892
Status: open
Priority: 0/
Queue: HTML-FormFu-Model-DBIC

People
Owner: Nobody in particular
Requestors: mzrinsky [...] redanvil.net
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.06000
Fixed in: (no value)



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 ) = @_;
On Thu Jan 21 14:35:19 2010, pldoh wrote: Show quoted text
> 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.
Sorry it's taken so long to respond to this! This is intended behaviour - it's happening because there's no field in the 'bar' fieldset with the name 'myVal'. Model::DBIC doesn't just use the names in $form->valid() - it maps the form fields to DBIC columns/methods. To get this to work, you would have to add an element to the 'bar' fieldset with the appropriate name. Carl
I was just confused then by the behavior of add_valid and params. Calling add_valid will cause the field name / value to appear in calls to params regardless of if the element exists in the form, so I just assumed that the behavior of model::dbic would be to just save those values. Perhaps add_valid in formfu should throw an exception then if the field does not exist? If i understand you correctly you are saying that when calling $form->add_valid() it should only override the value if the field actually exists in $form? Otherwise if there is a separation there, i.e. formfu allows that, where as model::dbic is only intended to crawl $form elements saving their values, (i.e. not just save all submitted values), then i can understand that as well, just did not get that from the docs. Either way, Thanks!