0

「開始」日付フィールドが「終了」日付フィールドと異なることを確認したい。だから私はこのドキュメント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,
    });


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

しかし、関数「check_date」は検出されませんでした。

4

1 に答える 1

0

これが答えです:私は「定数」ではなく「バリデーター」を使用しました:私はこのドキュメント 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 (開始日と終了日のフォームフィールドを検証しました;))

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 に答える