Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform selected option #83

Open
nicolasfranck opened this issue Oct 23, 2014 · 2 comments
Open

transform selected option #83

nicolasfranck opened this issue Oct 23, 2014 · 2 comments

Comments

@nicolasfranck
Copy link

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?

@gshank
Copy link
Owner

gshank commented Oct 24, 2014

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.

@nicolasfranck
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants