gshank/html-formhandler

transform selected option

Opened this issue · 2 comments

It's possible to transform the selected option from a Select,
but afterwards the transformed value is used as fif.
Result: the selected option is not selected anymore.
Setting the attribute "fif_from_value" does not help.

Any idea?

I’m not exactly sure what you mean by “transform the selected option”, but
normally it doesn’t work well to set ‘selected’ by hand in an option
attribute. Usually you want to do that by setting the default value or
passing it in an init_object or something. If it’s set by hand in the
options, the rest of FH doesn’t know about it.

Gerda

On Thu, Oct 23, 2014 at 4:46 AM, Nicolas Franck notifications@github.com
wrote:

It's possible to transform the selected option from a Select,
but afterwards the transformed value is used as fif.
Result: the selected option is not selected anymore.
Setting the attribute "fif_from_value" does not help.

Any idea?


Reply to this email directly or view it on GitHub
#83.

This is what I'm trying to do (see below for the field source code)

  1. generate a select field "date", containing these options:

    [
    {
    "label": "tomorrow",
    "value": "+1 day"
    },
    {
    "label": "next week",
    "value": "+1 week"
    }
    ]

  2. the selected valued is used as a duration to be added to the current timestamp:

    • value "+1 week" is selected
    • the value is transformed:

    DateTime->now()->add( weeks => 1 )->strftime("%Y-%M-%DT%H:%M:%S")

    • The key "date" in FIF (fill-in-form) contains the timestamp now.

    e.g. 2014-10-01T14:00:00

  3. But when form validation fails, and the form must be rendered, HF uses the same FIF to fill in the values of the rendered fields. As "+1 week" is not the same as "2014-10-01T14:00:00", HF fails to preselect the option "+1 week", and selects the first option, instead of the second.

So there is no distinction between submitted values and FIF?

SOURCE:

 package HTML::FormHandler::Field::DateAdd;
 use HTML::FormHandler::Moose;
 use DateTime;
 use DateTime::Format::Strptime qw();
 use DateTime::TimeZone;
 extends 'HTML::FormHandler::Field::Select';
 our $VERSION = '0.01';
 has 'format' => (
     is => 'ro',
     isa => 'Str',
     required => 1,
     lazy => 1,
     default => sub { "%Y-%m-%dT%H:%M:%SZ" }
 );
 has 'time_zone' => (
     is => 'ro',
     isa => 'Str',
     lazy => 1,
     default => sub { "UTC" }
 );
  sub get_class_messages {
    my $self = shift;
    return { %{ $self->next::method },%$class_messages };
  }
  our $class_messages = {
         'invalid_duration' => 'Invalid duration',
  };

  apply(
     [
         {
         transform => sub {
             my $value = $_[0];
             $value =~ s/^\s+//o;
             $value =~ s/\s+$//o;
             return $value;
         }
     },
     {
         check => sub {
             $_[0] =~ /^[+-]?\d+\s+(?:second|minute|hour|day|week|month|year)$/;
         },
         message => sub {
             my ( $value, $field ) = @_;
             return $field->get_message('invalid_duration');
         }
     },
     {
         transform => sub {
             my($value,$field)=@_;
             my($num,$duration) = split(/\s+/,$value);  
             $duration .= "s";
             my $date = DateTime->now()->add($duration => $num);
             if(defined($field->time_zone())){
                 $date->set_time_zone($field->time_zone());
             }
             DateTime::Format::Strptime::strftime($field->format,$date);
         }
     }
  );
  **PACKAGE**->meta->make_immutable;
  use namespace::autoclean;
  1;