Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COthank you for your reply. Well the reason why I wanted to avoid a cpan modules is that in Ruby it would take me 1 line per field e.g. email validation if self.email == '' errors << 'email cannot be blank' elsif !(email !=~ /\A\Z|\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i) errors << 'email must have a valid format' is it really *that* complicate to do the same thing in perl e.g. another template, cpan module + separate file to define the validation criteria? I just want to be sure I am not missing something as simple as in ruby... thanks
      singulars
    2. COIf you really think it's that easy in Ruby, then you can do it the exact same way in Perl. Instead of `errors << 'message here'` in Perl you might say `add_error('message here')` and define the `add_error` helper function to push messages onto a global array. Ultimately that's what Data::Form::Validator is doing, but in a more structured and reusable way (e.g.: it has a rule for email addresses so you don't need a regex). Also in my view it simplifies the logic to have one helper that returns a full set of valid input or nothing at all.
      singulars
    3. COgrant, thank you so much. If I am not asking too much and to the benefit of other future viewers could you refine your reply with a sample validation using data::validator as follow (username, email, password, motivation) assuming username check should be existing, email the sytnax and existing, password matching 2 fields and motivation a certain length? I think it would be helfpful! If is too much asked I understand
      singulars
 

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