Form::Tiny::Plugin::Diva - Form::Diva integration for Form::Tiny
### Form configuration use Form::Tiny plugins => ['Diva']; # Form::Diva configuration can be passed using diva_config diva_config label_class => 'my-label-class'; # data of fields is used as an input to Form::Diva # note: name is added automatically from field name form_field 'normal_edit' => ( data => {type => 'text'}, ); # passing type => 'hidden' will make the field hidden in diva form_field 'hidden' => ( data => {type => 'hidden'}, ); # if there is no data section at all, the field is also treated as hidden # default value in form is used form_field 'also_hidden' => ( default => sub { 55 }, ); ### Using Form::Diva my $form = MyFormPackage->new; $form->set_input({normal_edit => 'edited!'}); # Form::Diva adapter - see Form::Diva docs my $diva = $form->diva; # no need to pass the data in first argument to functions that need it print Dumper($form->generated);
This plugin adds some HTML outputting capabilities to Form::Tiny forms using Form::Diva.
This plugin can be added to Form::Tiny with the following line:
use Form::Tiny plugins => ['Diva'];
By default, these values will be passed to Form::Diva constructor:
id_base => 'form-field-', label_class => 'form-label', input_class => 'form-control', error_class => 'invalid-feedback',
These can be changed by a call to a new diva_config DSL keyword:
diva_config
# one or more at a time diva_config label_class => 'form-label', input_class => 'form-control';
All fields defined in the form will be passed into Form::Diva constructor:
fields with no data attribute will be used as hidden fields
data
fields with data attribute must have it as a hash reference and will be used as regular fields, unless type => 'hidden' is specified
type => 'hidden'
Contents of data are documented in "form" in Form::Diva. A couple of notes:
name will be automatically copied over from Form::Tiny field name, so there's no need to write it out loud
name
default will be copied over from Form::Tiny default, but only if not explicitly passed
default
comment is reserved for internal use
comment
You can get a preconfigured diva object by calling diva method on your form instance:
diva
my $diva = $form_instance->diva;
This is a subclass of Form::Diva, and it behaves slightly differently. The main difference is that you no longer need to pass the data explicitly into methods like generate or prefill, as the form input will be used by default:
generate
prefill
# no arguments, yet will use $form_instance->input my $generated = $diva->generated;
Additionally, all generated fields will have an extra hash key, errors which will contain the string with errors ready to be put into HTML:
errors
my $errors = $generated->[0]{errors};
When there are no errors, this value will be empty (but never undefined). You can reliably use it with your template engine to print errors. Note that no form scope errors are included - for those you will have to call another method form_errors on diva adapter:
form_errors
my $global_errors = $diva->form_errors;
You can configure the HTML class of error containers with error_class configuration field (see above).
error_class
No support for nesting - neither nested arrays nor hashes
Form should print without problems, but will not accept the data back from HTML without modification.
Occupies 'comment' field from Form::Diva
Comment field is needed to pass metadata through the generation mechanism. Contents of this field explicitly configured in forms will be discarded.
Form::Tiny
Form::Diva
Bartosz Jarzyna, <bbrtj.pro@gmail.com>
Copyright (C) 2022 by Bartosz Jarzyna
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.34.0 or, at your option, any later version of Perl 5 you may have available.
To install Form::Tiny::Plugin::Diva, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Form::Tiny::Plugin::Diva
CPAN shell
perl -MCPAN -e shell install Form::Tiny::Plugin::Diva
For more information on module installation, please visit the detailed CPAN module installation guide.