0

我想检查“从”日期字段与“到”日期字段是否不同。所以我有这个文档HTML::FormFu::Constraint::Callback

配置.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,
    });


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

但它没有检测到函数“check_date”。

4

1 回答 1

0

这是答案:我使用了“验证器”而不是“常量”:我遵循了这个文档 HTML::FormFu::Manual::Cookbook

配置.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(我有验证开始和结束日期表单字段;))

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;
于 2013-10-30T10:36:03.100 回答