Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Data::FormValidator question

by freddo411 (Chaplain)
on Aug 30, 2005 at 20:05 UTC ( [id://487929]=perlquestion: print w/replies, xml ) Need Help??

freddo411 has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

The Data::FormValidator docs say:
====

field_filters field_filters => { cc_no => ['digit'], },
A hash ref with field names as keys. Values are array references of built-in filters to apply (trim,digit,etc) or an anonymous subroutine which should take one parameter, the field value and return the (possibly) modified value.

=====

OK so far, I want to use the mentioned anonymous sub. I'm not sure on the syntax. (How do I specify the input to pass in?) I set up a working sub MyFilter that takes one param. I tried:

field_filters => { name_field => [ \MyFilter() ], },
That doesn't work, which doesn't surprize me as I don't see how MyFilter is getting its input.

Your comments and a working example would be much appreciated.

UPDATE: minor edit for a more illustrative field name
UPDATE 2: Working example from [monk://Tanktalus] :

field_filters => { name_field => [ \&MyFilter ], },

Cheers

--Fred

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Data::FormValidator question
by Tanktalus (Canon) on Aug 30, 2005 at 20:16 UTC

    field_filters => { my_filter => [ \&MyFilter ], },
    or...
    field_filters => { my_filters => [ sub { ...code here... } ], },
    Or, as a variation on the latter one:
    field_filters => { my_filters => [ sub { MyFilter(@_) } ], },
    The first one is great if you have a named sub to pass in anyway. But I like the second one since I'm creating a sub solely for passing around as an argument like this anyway.

Re: Data::FormValidator question
by davidrw (Prior) on Aug 30, 2005 at 20:19 UTC
    I think it's referring to something like this:
    field_filters => { my_filter => sub { my $value = shift; $value =~ s/foo/bar; return + $value; }, },
    filters => [ sub { my $value = shift; $value =~ s/foo/bar; return $val +ue; } ],
    You can probably do something like this as well:
    sub some_filter_sub { my $value = shift; $value =~ s/foo/bar; return $value; } # field_filters => { # my_filter => \&some_filter_sub, # }, filters => [ my_filter => \&some_filter_sub ],
    Update: corrected my misreading of the question and the docs ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://487929]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-28 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found