HTML::FormHandlerX::Field::JavaScript - a script tag with javascript code supplied via field for HTML::FormHandler.
version 0.003
This class can be used for fields that need to supply JavaScript code to control or modify the form. It will render the value returned by a form's js_code_<field_name> method, or the field's js_code attribute.
js_code_<field_name>
js_code
has_field 'user_update' => ( type => 'JavaScript', js_code => q`$('#fldId').on('change', myFunction);` );
or using a method:
has_field 'user_update' => ( type => 'JavaScript' ); sub js_code_user_update { my ( $self, $field ) = @_; if( $field->value == 'foo' ) { return q`$('#fldId').on('change', myFunction);`; } else { return q`$('#otherFldId').on('change', myOtherFunction);`; } } #---- has_field 'usernames' => ( type => 'JavaScript' ); sub js_code_usernames { my ( $self, $field ) = @_; return q`$('#fldId').on('change', myFunction);`; }
or set the name of the code generation method:
has_field 'user_update' => ( type => 'JavaScript', set_js_code => 'my_user_update' ); sub my_user_update { .... }
or provide a 'render_method':
has_field 'user_update' => ( type => 'JavaScript', render_method => \&render_user_update ); sub render_user_update { my $self = shift; .... return q( <script type="text/javascript"> // code here </script>); }
The code generation methods should return a scalar string which will be wrapped in script tags. If you supply your own 'render_method' then you are responsible for calling $self->wrap_data yourself.
$self->wrap_data
We support the following additional field options, over what is inherited from HTML::FormHandler::Field
String containing the JavaScript code to be rendered inside script tags.
Name of method that gets called to generate the JavaScript code.
Boolean to indicate whether code should be minified using JavaScript::Minifier::XS
The following methods can be called on the field.
The wrap_js_code method minifies the JavaScript code and wraps it in script tags.
wrap_js_code
Charlie Garrison <garrison@zeta.org.au>
This software is copyright (c) 2014 by Charlie Garrison.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install HTML::FormHandlerX::Field::JavaScript, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::FormHandlerX::Field::JavaScript
CPAN shell
perl -MCPAN -e shell install HTML::FormHandlerX::Field::JavaScript
For more information on module installation, please visit the detailed CPAN module installation guide.