view release on metacpan or search on metacpan
"label" : "Name",
"type" : "testfield",
"name" : "name"
}
]
);
This way, you can build your forms dynamically (e.g. based on database
entries).
validate_form_fields
This helper validates the input. It uses the
Mojolicious::Validator::Validation and it validates all fields defined
in the configuration file.
For more details see Validation.
forms
This method returns a list of forms. That means the filenames of all
.json files in the configured directory.
my @forms = $controller->forms;
The form field (HTML)
* $id
The id for the field. If no id is defined, the name of the field is
set.
Validation
You can define some validation rules in your config file. And when you
call validate_form_fields, the fields defined in the configuration file
are validated.
Mojolicious::Validator::Validation is shipped with some basic
validation checks:
* in
* size
* like
{
"label" : "Name",
"type" : "text",
"validation" : {
"size" : [ 2, 5 ]
},
"name" : "name"
}
]
Then you can call validate_form_fields:
my %errors = $c->validate_form_fields( $config_name );
In the returned hash, you get the fieldnames as keys where a validation
check fails.
A mandatory string
If you have mandatory fields, you can define them as required
[
{
lib/Mojolicious/Plugin/FormFieldsFromJSON.pm view on Meta::CPAN
if ( $params and $params->{hash} ) {
return @fields_long;
}
return @fields;
}
);
$app->helper(
'validate_form_fields' => sub {
my ( $c, $file ) = @_;
return '' if !$file;
if ( !$configs{$file} ) {
$self->_load_config_from_file( $c, \%configs, $file );
}
return '' if !$configs{$file};
lib/Mojolicious/Plugin/FormFieldsFromJSON.pm view on Meta::CPAN
{
"label" : "Name",
"type" : "testfield",
"name" : "name"
}
]
);
This way, you can build your forms dynamically (e.g. based on database entries).
=head2 validate_form_fields
This helper validates the input. It uses the L<Mojolicious::Validator::Validation> and it
validates all fields defined in the configuration file.
For more details see L<Validation|Mojolicious::Plugin::FormFieldsFromJSON/Validation>.
=head2 forms
This method returns a list of forms. That means the filenames of all .json files
in the configured directory.
my @forms = $controller->forms;
lib/Mojolicious/Plugin/FormFieldsFromJSON.pm view on Meta::CPAN
The form field (HTML)
=item * $id
The id for the field. If no id is defined, the name of the field is set.
=back
=head1 Validation
You can define some validation rules in your config file. And when you call C<validate_form_fields>, the
fields defined in the configuration file are validated.
L<Mojolicious::Validator::Validation> is shipped with some basic validation checks:
=over 4
=item * in
=item * size
=item * like
lib/Mojolicious/Plugin/FormFieldsFromJSON.pm view on Meta::CPAN
{
"label" : "Name",
"type" : "text",
"validation" : {
"size" : [ 2, 5 ]
},
"name" : "name"
}
]
Then you can call C<validate_form_fields>:
my %errors = $c->validate_form_fields( $config_name );
In the returned hash, you get the fieldnames as keys where a validation check fails.
=head2 A mandatory string
If you have mandatory fields, you can define them as required
[
{
"label" : "Name",
t/validation/01_textfield.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=t')->status_is(200)->content_is('Not ok');
$t->get_ok('/?name=tester')->status_is(200)->content_is('Not ok');
done_testing();
t/validation/02_textfield_required.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/')->status_is(200)->content_is('Not ok');
done_testing();
t/validation/03_unset_optional.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
done_testing();
t/validation/04_unset_required.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Not ok');
done_testing();
t/validation/05_error_message.t view on Meta::CPAN
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => $errors{name} || '');
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('');
$t->get_ok('/?name=t')->status_is(200)->content_is('length must be between 2 and 5 chars');
$t->get_ok('/?name=tester')->status_is(200)->content_is('length must be between 2 and 5 chars');
done_testing();
t/validation/06_specific_error_message.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my ($field,$msg) = $c->validate_form_fields( $config_name );
$c->render(text => $msg || '');
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('');
$t->get_ok('/?name=t')->status_is(200)->content_is("text must contain 'es'");
$t->get_ok('/?name=tester')->status_is(200)->content_is('length must be between 2 and 5 chars');
done_testing();
t/validation/07_mixed_errors.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my ($field,$msg) = $c->validate_form_fields( $config_name );
$c->render(text => $msg || '');
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('');
$t->get_ok('/?name=t')->status_is(200)->content_is("1");
$t->get_ok('/?name=tester')->status_is(200)->content_is('length must be between 2 and 5 chars');
done_testing();
t/validation/08_required_error_message.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my ($field,$msg) = $c->validate_form_fields( $config_name );
$c->render(text => $msg || '');
};
my $t = Test::Mojo->new;
$t->get_ok('/')->status_is(200)->content_is('text is required');
$t->get_ok('/?name=tester')->status_is(200)->content_is('');
done_testing();
t/validation/09_required_no_error_message.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my ($field,$msg) = $c->validate_form_fields( $config_name );
$c->render(text => $msg || '');
};
my $t = Test::Mojo->new;
$t->get_ok('/')->status_is(200)->content_is('1');
$t->get_ok('/?name=tester')->status_is(200)->content_is('');
done_testing();
t/validation/10_change_value.t view on Meta::CPAN
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
$c->param('name', 'test');
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=tester')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=t')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
done_testing();
t/validation/11_textfield_filters.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=test&name=hallo')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=%20%20%20%20test%20')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=%20%20%20%20%20test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=test%20%20%20%20%20')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=t')->status_is(200)->content_is('Not ok');
t/validation/12_textfield_required_filters.t view on Meta::CPAN
plugin 'FormFieldsFromJSON' => {
dir => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
};
my $config_name = basename __FILE__;
$config_name =~ s{\A \d+_ }{}xms;
$config_name =~ s{\.t \z }{}xms;
get '/' => sub {
my $c = shift;
my %errors = $c->validate_form_fields( $config_name );
$c->render(text => ( %errors ? 'Not ok' : 'Everything ok' ) );
};
my $t = Test::Mojo->new;
$t->get_ok('/?name=test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=%20%20%20%20test')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=test%20%20%20%20')->status_is(200)->content_is('Everything ok');
$t->get_ok('/?name=%20%20test%20%20')->status_is(200)->content_is('Everything ok');
$t->get_ok('/')->status_is(200)->content_is('Not ok');