Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure why you'd want to expressly avoid using a plugin or a CPAN module - any solution you come up with will likely do the the same things as a plugin and/or CPAN module except that since it's new code it won't be as mature and tested.</p> <p>I've recently started using Dancer to develop a simple app and I'll describe how I'm solving this problem using CPAN modules. You may of course choose to ignore it.</p> <p>My route handler for adding a user looks like this:</p> <pre><code>post '/user/add' =&gt; { my $args = valid_input('/user/add') or return template('user/add'); my $user = User-&gt;new({ username =&gt; $args-&gt;{username}, # ... }); user-&gt;insert; # you'll probably want some error handling here set_flash('User added'); redirect '/user'; }; </code></pre> <p>The <code>valid_input</code> function is a helper I created for my app, which uses the <a href="https://metacpan.org/release/Data-FormValidator" rel="nofollow">Data::Form::Validator</a> module from CPAN. The <code>'/usr/add'</code> argument is the name of the validation profile to use - which for simplicity I decided to keep the same as the route name.</p> <p>I won't bore you with the details of calling <a href="https://metacpan.org/release/Data-FormValidator" rel="nofollow">Data::Form::Validator</a> since the documentation is pretty good. What I will mention is that in the event that validation fails, the <code>valid_input</code> helper stores away the validation error messages for display in an alert box and also saves the submitted parameters:</p> <pre><code>my $q = params(); # need to force scalar context to get a hashref; fill_in_form($q); # save submitted parameters </code></pre> <p>I have a <code>before_template_render</code> hook that makes the saved validation error messages available to the template (they actually get rendered in <code>views/layouts/main.tt</code>).</p> <p>I also have an <code>after_template_render</code> hook that takes the saved submitted parameters and puts them back into the rendered HTML form using the <a href="https://metacpan.org/release/HTML-FillInForm" rel="nofollow">HTML::FillInForm</a> module.</p> <p>Since I have that infrastructure in place, the route handler to display an existing user record in an edit form with all the fields pre-populated is trivial:</p> <pre><code>get '/user/:id' =&gt; sub { if( my $user = User-&gt;find( param('id') ) ) { fill_in_form($user); return template 'user/edit', { id =&gt; $user-&gt;id }; } not_found "User not found"; }; </code></pre> <p>The <code>User</code> in <code>User-&gt;find()</code> is another one of my app-specific helper functions. It uses the <a href="https://metacpan.org/release/Dancer-Plugin-DBIC" rel="nofollow">Dancer::Plugin::DBIC</a> module to return a resultset object for my <code>user</code> table.</p> <p>I will say that I'm not necessarily a huge fan of Data::Form::Validator. I have been able to achieve everything I wanted but sometimes it does seem to take more work than I'd like.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload