Data::FormValidator 打印无效数据的 hashref 而不是字符串
我们开发了一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表单验证器选项的
msgs->{invalid}
键是一个哈希值。 它是一个字符串:您应该能够在
constraints
键中使用>msgs 每个无效字段错误消息的哈希值:但是,我目前也在尝试使用
约束
并且它没有任何效果,因此字段相关错误消息的功能可能不会实际存在。The
msgs->{invalid}
key for your form validator options is a hash. It is meant to be a string:You should be able to use the
constraints
key in themsgs
hash for per invalid field error messages: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.它是你的 html 中的一个拼写错误:
应该阅读
Its a typo in your html:
should read