Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:42:44+00:00 2026-06-11T04:42:44+00:00

I am trying to do a simple form validation in perl dancer but I

  • 0

I am trying to do a simple form validation in perl dancer but I was wondering what would be the best way to validate simple parameters (e.g. field cannot be empty, validity of the email, minimum length of a field) in dancer/perl without any extra plugin or CPAN module

here is the code so far

post '/register' => sub {

my $db = connect_db();
my $sql = 'insert into users (username, email, password, motivation) values (?, ?, ? ,?)';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'username'}, params->{'email'},params->{'password'}, params->{'motivation'}) or die $sth->errstr;

set_flash('Hey you signed up !');
redirect '/thanks';
};

I did google it and I found several ways to do validation using CPAN modules like Form::Foo but how do it without that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:42:45+00:00Added an answer on June 11, 2026 at 4:42 am

    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.

    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.

    My route handler for adding a user looks like this:

    post '/user/add' => {
        my $args = valid_input('/user/add')
            or return template('user/add');
        my $user = User->new({
            username => $args->{username},
            # ...
        });
        user->insert;  # you'll probably want some error handling here
        set_flash('User added');
        redirect '/user';
    };
    

    The valid_input function is a helper I created for my app, which uses the Data::Form::Validator module from CPAN. The '/usr/add' argument is the name of the validation profile to use – which for simplicity I decided to keep the same as the route name.

    I won’t bore you with the details of calling Data::Form::Validator since the documentation is pretty good. What I will mention is that in the event that validation fails, the valid_input helper stores away the validation error messages for display in an alert box and also saves the submitted parameters:

    my $q = params();  # need to force scalar context to get a hashref;
    fill_in_form($q);  # save submitted parameters
    

    I have a before_template_render hook that makes the saved validation error messages available to the template (they actually get rendered in views/layouts/main.tt).

    I also have an after_template_render hook that takes the saved submitted parameters and puts them back into the rendered HTML form using the HTML::FillInForm module.

    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:

    get '/user/:id' => sub {
        if( my $user = User->find( param('id') ) ) {
            fill_in_form($user);
            return template 'user/edit', { id => $user->id };
        }
        not_found "User not found";
    };
    

    The User in User->find() is another one of my app-specific helper functions. It uses the Dancer::Plugin::DBIC module to return a resultset object for my user table.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a simple input box with form validation, but im not
I'm trying to make a simple form, but it's working not so fine. This
I'm trying to validate a simple form in JSP with Spring and Hibernate using
I am trying to set up validation on a simple contact form that is
I am trying to write a simple input field validation plugin at the moment
I am trying to add some simple validation to my asp.net mvc form and
I am trying to do some very simple form validation checking for null or
I'm currently trying simple validation using required=true <h:form> <h:messages globalOnly=true/> <h:panelGrid> <h:panelGrid columns=3> <f:facet
I have a simple form with fields that I am trying to validate using
I'm trying to make a simple Form Validation so the Form shows errors This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.