Data::FormValidator 打印无效数据的 hashref 而不是字符串

发布于 2024-10-31 10:49:31 字数 1649 浏览 1 评论 0原文

我们开发了一个 Catalyst 应用程序,我的同事开始使用 Catalyst::Plugin::FormValidator 实现表单验证,它提供了 Data::FormValidator 的快捷方式。

验证似乎成功,并且缺失条目的显示有效。但是,当显示不匹配约束的详细信息时,页面包含一个哈希引用,例如 HASH(0x784d80)

这是我们用来配置验证器的哈希(首先测试 FormValidator 的所有示例代码):

{
    required => [qw/email age sex/],
    optional => [qw/city name/],
    filters  => [qw/trim/],

    constraints => {
        name => sub { length $_[1] >= 2 },
        sex => sub  { $_[1] =~ /^(male)|(female)$/},
        age => sub { $_[1] =~ /^[0-9]{1,2}$/ },
        email => sub { Email::Valid->address(lc($_[1])) },
        city => sub { length $_[1] >= 5 },
    },

    msgs => {
        invalid => {
            field => {
                email => 'no valid e-mail address',
                age => 'no valid age between 01 and 99',
                },

                default => 'contains an invalid value',
            },

        missing => 'missing!',
    },

}

这就是我们读取值的方式:

    <tr>
        <td>E-mail address:</td>
        <td><input type="text" name="email" value="[% email %]" />* [% c.form.msgs.email %]</td>
    </tr>

对于缺失的字段,一切正常。对于无效字段,仅打印出赋予 invalid => 的值。如果设置为无效=> “THIS IS INVALID!” 会打印字符串,如果设置为 invalid =>; {} 哈希引用打印为 HASH(0x784d80)(即使是空哈希)。

有人可以解释这是为什么吗?我们使用 CPAN 的 Data::FormValidator 当前版本 4.66。

更新:通过转储[% c.forms %],我的同事可以验证字段的有效性是否已正确确定。只是字符串设置错误,我们不知道为什么。

We develop a Catalyst app and my coworker started implementing form validation with Catalyst::Plugin::FormValidator which provides a shortcut to Data::FormValidator.

The validation seems to succeed and the display of missing entries works. But when displaying details for mismatched constraints the page contains a hashref like HASH(0x784d80).

This is the hash we use to configure the validator (everything example code made up to test FormValidator first):

{
    required => [qw/email age sex/],
    optional => [qw/city name/],
    filters  => [qw/trim/],

    constraints => {
        name => sub { length $_[1] >= 2 },
        sex => sub  { $_[1] =~ /^(male)|(female)$/},
        age => sub { $_[1] =~ /^[0-9]{1,2}$/ },
        email => sub { Email::Valid->address(lc($_[1])) },
        city => sub { length $_[1] >= 5 },
    },

    msgs => {
        invalid => {
            field => {
                email => 'no valid e-mail address',
                age => 'no valid age between 01 and 99',
                },

                default => 'contains an invalid value',
            },

        missing => 'missing!',
    },

}

This is how we read the values:

    <tr>
        <td>E-mail address:</td>
        <td><input type="text" name="email" value="[% email %]" />* [% c.form.msgs.email %]</td>
    </tr>

For missing fields everything works fine. For invalid fields the value given to invalid => is just printed out. If set to invalid => "THIS IS INVALID!" the string is printed and if set to invalid => {} the hashref is printed as e.g. HASH(0x784d80) (even for empty hash).

Can somebody explain why this is? We use the current version 4.66 of Data::FormValidator from CPAN.

Update: By dumping [% c.forms %] my coworker could verify that the validity of the fields is determined correctly. Just the string is set wrong and we don't know why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-11-07 10:49:31

表单验证器选项的 msgs->{invalid} 键是一个哈希值。 它是一个字符串

这是一个更复杂的示例,展示了如何提供您自己的默认消息字符串,以及如何为每个字段提供自定义消息以及处理多个约束:

msgs =>; {
    ...

    # 默认无效消息,默认为“Invalid”
    无效=> '有问题!',

    ...
}

您应该能够在 constraints 键中使用>msgs 每个无效字段错误消息的哈希值:

constraints => {
    'date_and_time' => 'Not a valid time format',
    # ...
}

但是,我目前也在尝试使用约束并且它没有任何效果,因此字段相关错误消息的功能可能不会实际存在。

The msgs->{invalid} key for your form validator options is a hash. It is meant to be a string:

Here's a more complex example that shows how to provide your own default message strings, as well as providing custom messages per field, and handling multiple constraints:

msgs => {
    ...

    # Default invalid message, default's to "Invalid"
    invalid => 'Problematic!',

    ...
}

You should be able to use the constraints key in the msgs hash for per invalid field error messages:

constraints => {
    'date_and_time' => 'Not a valid time format',
    # ...
}

However, I am also currently trying to use the constraints and it is not having any effect, so the feature of field dependent error messages may not actually exist.

妄司 2024-11-07 10:49:31

它是你的 html 中的一个拼写错误:

[% c.form.msgs.email %]

应该阅读

[% c.form.msgs.invalid.field.email %]

Its a typo in your html:

[% c.form.msgs.email %]

should read

[% c.form.msgs.invalid.field.email %]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文