Skip to content

Commit

Permalink
more documentation for validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 28, 2013
1 parent 1215a07 commit dfad27f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -910,7 +910,7 @@ that this method is EXPERIMENTAL and might change without warning!
my $validation = $c->validation;
$validation->required('title')->size(3, 50);
$validation->optional('description')->size(1, 250);
my $title = $validation->param('title');
=head2 write
Expand Down
91 changes: 90 additions & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -648,7 +648,7 @@ you can perform checks on their values.
<head>
%= stylesheet begin
.fields_with_errors { display: inline }
.fields_with_errors input { background-color: #ffbbbb }
.fields_with_errors input { background-color: #fd9e7e }
% end
</head>
<body>
Expand Down Expand Up @@ -677,9 +677,98 @@ styling with CSS easier.
<input type="text" name="user" value="sri" />
</div>

Every check is performed right away, so you can use the results immediately to
build more advanced validation logic with methods like
L<Mojolicious::Validator::Validation/"is_valid">.

use Mojolicious::Lite;

# /test -> "No data."
# /test?foo=one -> "First choice won with one."
# /test?bar=two -> "Alternative won with two."
# /test?foo=o&bar=three -> "Alternative won with three."
# /test?baz=four -> "Try again."
get '/test' => sub {
my $self = shift;

my $validation = $self->validation;
return $self->render(text => 'No data.') unless $validation->has_data;

# First choice
if ($validation->optional('foo')->size(3, 5)->is_valid) {
my $foo = $validation->param('foo');
$self->render(text => "First choice won with $foo.");
}

# Alternative
elsif ($validation->optional('bar')->in(qw(one two three))->is_valid) {
my $bar = $validation->param('bar');
$self->render(text => "Alternative won with $bar.");
}

else { $self->render(text => 'Try again.') }
};

app->start;

For a full list of available checks see also
L<Mojolicious::Validator/"CHECKS">.

=head2 Adding form validation checks

There are two parts to every validation check, the check itself and an
optional generator for dynamic error messages, they can be added with the
methods L<Mojolicious::Validator::Validation/"add_check"> and
L<Mojolicious::Validator::Validation/"add_error">.

use Mojolicious::Lite;

# Add "range" check
app->validator->add_check(range => sub {
my ($validation, $name, $value, $min, $max) = @_;
return $value >= $min && $value <= $max;
});

# Add "range" error generator
app->validator->add_error(range => sub {
my ($validation, $name, $value, $min, $max) = @_;
return "Value needs to be between $min and $max.";
});

get '/' => sub {
my $self = shift;

my $validation = $self->validation;
return $self->render unless $validation->has_data;

$validation->required('test')->range(3, 23);

$self->render(text => 'Success!') unless $validation->has_error;
} => 'index';

app->start;
__DATA__

@@ index.html.ep
<!DOCTYPE html>
<html>
<head>
%= stylesheet begin
.fields_with_errors { display: inline }
.fields_with_errors input { background-color: #fd9e7e }
% end
</head>
<body>
%= form_for index => begin
% if (validation->has_error('test')) {
<p><%= validation->errors('test') %></p>
% }
%= text_field 'test'
%= submit_button
% end
</html>
</html>

=head2 Adding helpers

Adding and redefining helpers is very easy, you can use them to do pretty much
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -721,7 +721,8 @@ For a full list of available hooks see L<Mojolicious/"hook">.

=head2 Shortcuts

You can also add your own shortcuts to make route generation more expressive.
You can also add your own shortcuts with L<Mojolicious::Routes/"add_shortcut">
to make route generation more expressive.

# Simple "resource" shortcut
$r->add_shortcut(resource => sub {
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -166,6 +166,9 @@ Perform validation check.
Set custom error message for next validation C<check> or C<topic> change.
$validation->optional('foo')
->error('Name needs to be between 3 and 50 characters.')->size(3, 50);
=head2 errors
my $collection = $validation->errors('foo');
Expand Down

0 comments on commit dfad27f

Please sign in to comment.