[html-formfu] Modifying a form field.

Carl Franks fireartist at gmail.com
Thu Apr 2 08:34:07 BST 2009


2009/4/1 Leanan Sidhe <the.leanan.sidhe at gmail.com>:
> I have heard that TIMTOWTDI, but the only suggestions I've seen were in
> regards to password fields and using DBIx::Class::DigestColumns.  I need to
> do some mathematical conversions on columns before the data is put into the
> database.  I'm trying to do it this way:
>
>
>
>   my $elements_to_convert = $form->get_all_elements({ type => 'Text'});
>
>   foreach my $element (@{$elements_to_convert}) {
>     if ($element->attributes->{'class'} =~ /I-want-to-convert-this/){
>       my $nested_name = $element->nested_name;
>       my $field_value = $form->param_value($nested_name);
>       $element->default(($field_value * $conversion));
>     }
>   }
>
> However, nothing is working.  I've tried $element->default($my_new_value),
> $form->default_values($nested_name => $my_new_value),
> $form->default_values({$nested_name => $my_new_value}) and
> $form->add_valid($nested_name, $my_new_value}). [snip]

That extraneous curly in add_valid() doesn't compile - so I'm not sure
whether you tried passing 2 args or a hash-ref.

add_valid() should work, but takes 2 args - ( $name, $value )

However, I would recommend you use a Transformer instead, then you can
just add it to each field in your config.
Using a Transformer rather than a filter means any constraints will be
run before the value is converted.

    elements:
      - name: foo
        transformers:
          - 'MyApp::ConvertFoo'

Something like this should do:

package HTML::FormFu::Transformer::MyApp::ConvertFoo;

use strict;
use base 'HTML::FormFu::Transformer';
use HTML::FormFu::Constants qw( $EMPTY_STR );

sub transformer {
    my ( $self, $value ) = @_;

    return $value if !defined $value || $value eq $EMPTY_STR;

    return $value * 'conversion';
}

1;



More information about the HTML-FormFu mailing list