Question

I want to check that the 'From' date field is different to the 'To' date field. So I have flow this doc HTML::FormFu::Constraint::Callback:

config.yml:

type: Text
      name: to
      label: To
      constraints:
          - type: DateTime
            parser:
                strptime: '%Y-%m-%d %H:%M:%S'
          - type: Callback
            callback: "check_date"

my_controller.pm:

 sub check_date {
        my ( $value, $params ) = @_;

        return 1; //juste fel testing
}

sub index : Path :Args(0) :FormConfig('config.yml'){
     ..........

     my $field = $form->get_element({type => 'Text', 'name' => 'to'});
      $field->constraint({
        type => 'Callback',
        callback =>  \&check_date,
    });


    ...........
}

But it didn't detect the function "check_date".

Was it helpful?

Solution

This is th answer: i have used "validators" not "constrant": i have follow this doc HTML::FormFu::Manual::Cookbook

config.yml:

type: Text
      name: to
      label: To
      constraints:
          - type: DateTime
            parser:
                strptime: '%Y-%m-%d %H:%M:%S
      validators:
        - '+folder::Validators::Validator' # don't  forget the '+' ;)

Validator.pm (i have validate start and end date form field ;))

package folder::Validators::Validator;
use strict;
use base 'HTML::FormFu::Validator';
use DateTime::Format::HTTP;

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

  my $from = DateTime::Format::HTTP->parse_datetime( $params->{'from'});
  my $to = DateTime::Format::HTTP->parse_datetime( $value);


  if (DateTime->compare( $from, $to ) == -1){
    return 1
  }

  die HTML::FormFu::Exception::Validator->new({
            message => 'This field is invalid',
        });
}

1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top