Skip to content

Commit

Permalink
explain form_for in the rendering guide
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 15, 2015
1 parent 1179af4 commit a3885b5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
48 changes: 44 additions & 4 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -829,6 +829,50 @@ template with named blocks that child templates can override.

This chain could go on and on to allow a very high level of template reuse.

=head2 Forms

To build HTML forms more efficiently you can use tag helpers like
L<Mojolicious::Plugin::TagHelpers/"form_for">, which can automatically select a
request method for you if a route name is provided. And since most browsers do
not allow request methods like C<PUT> or C<DELETE> for forms, they are spoofed
with a C<_method> query parameter.

use Mojolicious::Lite;

get '/' => 'form';

put '/title' => sub {
my $c = shift;

my $url = $c->param('url');
my $title = $c->ua->get($url)->res->dom->at('title')->text;

# Prevent double submit with redirect
$c->flash(title => qq{The title of "$url" is "$title".});
$c->redirect_to('form');
};

app->start;
__DATA__

@@ form.html.ep
<!DOCTYPE html>
<html>
<body>
% if (my $title = flash 'title') {
<p><%= $title %></p>
% }
%= form_for title => begin
%= text_field 'url', value => 'http://mojolicio.us'
%= submit_button 'Get title'
% end
</body>
</html>

The methods L<Mojolicious::Controller/"flash"> and
L<Mojolicious::Controller/"redirect_to"> are often used together to prevent
double form submission.

=head2 Form validation

You can use L<Mojolicious::Controller/"validation"> to validate C<GET> and
Expand Down Expand Up @@ -958,10 +1002,6 @@ L<Mojolicious::Validator::Validation/"error">.
</body>
</html>

The methods L<Mojolicious::Controller/"flash"> and

This comment has been minimized.

Copy link
@s1037989

s1037989 Mar 15, 2015

Contributor

How come this paragraph was removed? Is this no longer the case / no longer supported?

This comment has been minimized.

Copy link
@kraih

kraih Mar 15, 2015

Author Member

Moved, not removed.

L<Mojolicious::Controller/"redirect_to"> are often used together to prevent
double form submission.

=head2 Cross-site request forgery

CSRF is a very common attack on web applications that trick your logged in
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Guides/Tutorial.pod
Expand Up @@ -743,7 +743,8 @@ really secure.
All files uploaded via C<multipart/form-data> request are automatically
available as L<Mojo::Upload> objects. And you don't have to worry about memory
usage, because all files above 250KB will be automatically streamed into a
temporary file.
temporary file. To build HTML forms more efficiently, you can also use tag
helpers like L<Mojolicious::Plugin::TagHelpers/"form_for">.

use Mojolicious::Lite;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -363,7 +363,7 @@ Generate C<input> tag of type C<file>.
Generate portable C<form> tag with L<Mojolicious::Controller/"url_for">. For
routes that do not allow C<GET>, a C<method> attribute with the value C<POST>
will be automatically added. And for methods other than C<GET> or C<POST>, an
will be automatically added. And for methods other than C<GET> or C<POST>, a
C<_method> query parameter will be added as well.
<form action="/path/to/login">
Expand Down

0 comments on commit a3885b5

Please sign in to comment.