Hi Discipulus,

The main problem you have with the posted code is that you are trying to retrieve the value of your body parameters with the method for retrieving query parameters.

When you get data POSTed from a form, it's transported in the body of the HTTP request (in general, not specific to Dancer). Query parameters are those passed as the query string, e.g. /form?search_for=truth.

Dancer2 provides various ways to access your parameters.

The three methods for retrieving a certain class of params, body_parameters(), query_parameters(), route_parameters() all return a Hash::MultiValue object, so it is necessary to call a method to get values.

The reason for using Hash::MultiValue, and the reason these methods are recommended in the Dancer2 doc, is so you can, well, have multiple values. This is most commonly seen in query params, e.g.:

/form?multival=bar&multival=baz
To get all the values for multival you would use:
my @values = query_parameters->get_all('multival');
(see Hash::MultiValue).

The documentation for Dancer2 is, like most of the large frameworks, extensive and somewhat tricky to navigate. But it's all there in the manual:

Also:

I made a new pull request to your Github repo incorporating all the above suggestions into your app. The relevant route handler code is shown below.

Hope this helps!

any ['get', 'post'] => '/form' => sub { # POST request if ( request->method() eq "POST" ) { debug "method: POST"; debug 'All params: ' . Dumper { params }; debug 'One param from href: ' . params->{'search_for'}; debug 'Form params: ' . Dumper body_parameters->mixed; + # see Hash::MultiValue debug 'Param "search_for": ' . body_parameters->get('search_f +or'); debug 'Another way: ' . param 'search_for'; template 'form' => { title => 'Form Test', headline => 'form test POST', form_url => '/form', search_for => param 'search_for', # least typing ;-) }; } # GET request else { template 'form' => { title => 'Form Test', headline => 'formtest GET', form_url => '/form', }; } };


The way forward always starts with a minimal test.

In reply to Re: Dancer2 parameters understanding and usage by 1nickt
in thread Dancer2 parameters understanding and usage by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.