| 1 | package JavaScript::DataFormValidator; | ||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use vars (qw/@EXPORT $VERSION/); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | @EXPORT = (qw/ | |
| 7 | &js_dfv_profile | ||
| 8 | &js_dfv_onsubmit | ||
| 9 | /); | ||
| 10 | |||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | JavaScript::DataFormValidator - JavaScript form validation from a Perl Data::FormValidator profile | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | 2 | $VERSION = '0.50'; | |
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | This module helps with setting up a JavaScript validation for a form using | ||
| 23 | Data.FormValidator, a JavaScript port of L<Data::FormValidator>. A key feature | ||
| 24 | of this system is that it allows you to use the I<exact same> validation | ||
| 25 | profile for both Perl and JavaScript validation. | ||
| 26 | |||
| 27 | You should read the docs for the JavaScript implementation for some limitations: | ||
| 28 | http://www.openjsan.org/doc/u/un/unrtst/Data/FormValidator/ | ||
| 29 | |||
| 30 | Here's an example with HTML::Template syntax: | ||
| 31 | |||
| 32 | <script type="text/javascript" src="../JSAN/Data/FormValidator.js"></script> | ||
| 33 | |||
| 34 | <!-- tmpl_var dfv_profile --> | ||
| 35 | |||
| 36 | <form action="/foo" <tmpl_var js_dfv_onsubmit > > | ||
| 37 | <!-- ... -> | ||
| 38 | </form> | ||
| 39 | |||
| 40 | And then back in your perl code... | ||
| 41 | |||
| 42 | use JavaScript::DataFormValidator; | ||
| 43 | my $t = HTML::Template->new_file('foo.html'); | ||
| 44 | $t->param( | ||
| 45 | dfv_profile => js_dfv_profile( 'my_form' => { | ||
| 46 | required => [qw/email first_name/], | ||
| 47 | optional => 'last_name', | ||
| 48 | constraints => { | ||
| 49 | email => 'email', | ||
| 50 | } | ||
| 51 | }), | ||
| 52 | js_dfv_onsubmit => js_dfv_onsubmit('my_form'); | ||
| 53 | ); | ||
| 54 | |||
| 55 | =head2 REQUIREMENTS | ||
| 56 | |||
| 57 | The Data.FormValidator JavaScript file must be copied to your | ||
| 58 | server so that you can call it. Download the latest version | ||
| 59 | from here: | ||
| 60 | http://www.openjsan.org/doc/u/un/unrtst/Data/FormValidator/ | ||
| 61 | |||
| 62 | =head2 js_dfv_profile( $profile_name => \%profile_hash ); | ||
| 63 | |||
| 64 | $dfv_profile_in_js = js_dfv_profile( $profile_name => \%profile_hash ); | ||
| 65 | |||
| 66 | Takes a named Data::FormValidator profile in Perl, and returns a representation | ||
| 67 | of it in JavaScript, for use with the Data.FormValidator JavaScript module. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | =head2 STATUS | ||
| 72 | |||
| 73 | Hopefully, it's done. It's very simple code. | ||
| 74 | |||
| 75 | However, the API may break and change in the first weeks after the the release | ||
| 76 | as I get feedback. I'll plan to at least make a new release to remove this | ||
| 77 | notice once things seem stable. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | 1 | ▶ | sub js_dfv_profile { |
|
Subroutine
js_dfv_profile: called CC=1 SCAR=0.0
Pod
documented
| |||
| 82 | 1 | my ($name,$struct) = @_; | |
| 83 | 1 | require Data::JavaScript::Anon; | |
| 84 | 1 | my $js = Data::JavaScript::Anon->var_dump($name,$struct); | |
| 85 | 1 | return Data::JavaScript::Anon->script_wrap($js); | |
| 86 | } | ||
| 87 | |||
| 88 | =head2 js_dfv_onsubmit($profile_name); | ||
| 89 | |||
| 90 | $onsubmit_code = js_dfv_onsubmit($profile_name); | ||
| 91 | |||
| 92 | Returns the Javascript snippet to put in your <form> tag to call the basic | ||
| 93 | C<check_and_report()> JavaScript validation function. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | 1 | ▶ | sub js_dfv_onsubmit { |
|
Subroutine
js_dfv_onsubmit: called CC=1 SCAR=0.0
Pod
documented
| |||
| 98 | 1 | my $profile_name = shift; | |
| 99 | 1 | return qq{onSubmit="return Data.FormValidator.check_and_report(this, $profile_name);"}; | |
| 100 | } | ||
| 101 | |||
| 102 | # This would make things /really/ easy, but I'm | ||
| 103 | # =head2 js_dfv_functions(); | ||
| 104 | # | ||
| 105 | # $js_code = js_dfv_functions(); | ||
| 106 | # | ||
| 107 | # Returns the library of JavaScript functions and objects, in a script block. Useful for quick | ||
| 108 | # tests. For production use, it's recommended to share this code in it's own file and reference | ||
| 109 | # it through a c<<script>> tag . | ||
| 110 | |||
| 111 | =head1 FUTURE DEVELOPMENT | ||
| 112 | |||
| 113 | This module is mostly released as a demonstration of how to integrate with the | ||
| 114 | L<Data.FormValidator> JavaScript project. For anything more complicated, it | ||
| 115 | will probably be easier to use this source code as starting point for a custom | ||
| 116 | solution. | ||
| 117 | |||
| 118 | =head1 | ||
| 119 | |||
| 120 | =head1 AUTHOR | ||
| 121 | |||
| 122 | Mark Stosberg, C<< <mark at summersault.com> >> | ||
| 123 | |||
| 124 | =head1 BUGS | ||
| 125 | |||
| 126 | Please report any bugs or feature requests to | ||
| 127 | C<bug-cgi-application-plugin-javascript-formvalidator at rt.cpan.org>, or through the web interface at | ||
| 128 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JavaScript-DataFormValidator>. | ||
| 129 | I will be notified, and then you'll automatically be notified of progress on | ||
| 130 | your bug as I make changes. | ||
| 131 | |||
| 132 | =head1 SUPPORT | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item * CPAN Page | ||
| 137 | |||
| 138 | L<http://search.cpan.org/dist/JavaScript-DataFormValidator> | ||
| 139 | |||
| 140 | =item * RT: CPAN's request tracker | ||
| 141 | |||
| 142 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JavaScript-DataFormValidator> | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 ACKNOWLEDGEMENTS | ||
| 147 | |||
| 148 | This uses L<Data::JavaScript::Anon> for the heavy lifting. | ||
| 149 | |||
| 150 | =head1 COPYRIGHT & LICENSE | ||
| 151 | |||
| 152 | Copyright 2005 Mark Stosberg, all rights reserved. | ||
| 153 | |||
| 154 | This program is free software; you can redistribute it and/or modify it | ||
| 155 | under the same terms as Perl itself. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | 2 | 1; # End of JavaScript::DataFormValidator | |