[html-formfu] add_valid and inflators

Steve Caldwell info-formfu at caldwellhb.com
Thu May 20 23:52:19 GMT 2010


Hi all,

I have a form with two Select fields with custom inflators, e.g. a 
"city" field and a "state" field that inflate to a My::City and 
My::State object.  The values for the select fields are ids that are 
then passed to the database to inflate into the respective object.  I 
want to have code that, post-process, will override any submitted state 
value to match that of the city (if a city has been provided), e.g.:

# request has city=id_for_seattle_washington, state=id_for_arizona
$form->process($request);

# $form->param_value('city') is a My::City object
# $form->param_value('state') is a My::State object

...

# This will print "Arizona"
print $form->param_value('state')->name;

if ($form->param_value('city')) {
   $form->add_valid('state', $form->param_value('city')->state);
}

# Now this will print "Washington"
print $form->param_value('state')->name;

The above code all works fine, until I go to render the form.  The 
'state' dropdown does not have anything selected, neither Washington nor 
Arizona.  If I change the code so that we pass the id to add_valid(), e.g.:

...
if ($form->param_value('city')) {
   $form->add_valid('state', $form->param_value('city')->state_id);
}
...

The form renders fine but we choke on this line:

print $form->param_value('state')->name;

because param_value now returns a plain scalar holding the ID rather 
than an inflated object.  I guess this is because inflators are not run 
after calls to add_valid.

I got around this by adding code to look at the return of param_value 
and inflate on my own if it's not a ref to a My::State, but this is 
somewhat kludgy and involves a chunk of duplicate code.  Is there a more 
elegant way of doing this?

I should note that I also have deflators for both elements that do what 
you would expect, but they don't seem to help in this situation.

Thanks,
Steve



More information about the HTML-FormFu mailing list