Routing and Template Cookbook: Forms and processing

From Dreamwidth Notes
Revision as of 19:23, 24 February 2013 by Foxfirefey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Handlers

Authas forms

Here is an example of getting an authas form from the controller:

my ( $ok, $rv ) = controller( authas => 1 );
return $rv unless $ok;

The authas form is authas_html inside of $rv and can be passed into the template.

Form authentication

Just like above, form authentication in handled in the controller:

my ( $ok, $rv ) = controller( form_auth => 1 );
return $rv unless $ok;

If, for some justified reason, you have form submission and need there to be NO form authentication, you should explicitly do so:

my ( $ok, $rv ) = controller( form_auth => 0 );
return $rv unless $ok;

Make sure to comment why this needs to happen.

You can, of course, combine both authas and form_auth in the controller call:

my ( $ok, $rv ) = controller( authas => 1, form_auth => 1 );
return $rv unless $ok;

GET arguments

Contained in a request's get_args:

my $r = DW::Request->get;
$r->get_args->{id}->{core}

POST arguments

Contained in a request's post_args:

my $r = DW::Request->get;
$r->post_args->{core}

Templates

Authas forms

Authas forms--the forms that ask which journal/community a user is working on at the moment--should not be generated in the template. They should use the return value given by the controller above and passed in with the template variables:

[%- authas_html -%]

Form authentication

Any POST method form will need to include a form authentication:

[% dw.form_auth() %]

Form elements

Textbox

[%- form.textbox( label = dw.ml( '.email.user' )
        name = 'bounce_email'
        size = 25
)
-%]

Hidden

[% form.hidden( name = "parid_hidden", value = parid ) %]

Checkbox

[%- form.checkbox( label = dw.ml( ".put.in.queue" )
        name = "touch"
        id = "touch"
) -%]

Select

Here is an example that creates the items array that it passes it in:

[% '.createlayer.layoutspecific.label.type' | ml %]
[% layer_types = [
    "theme", dw.ml( '.createlayer.layoutspecific.select.theme' ),
    "i18n", dw.ml( '.createlayer.layoutspecific.select.language' ),
    "user", dw.ml( '.createlayer.layoutspecific.select.user2' ),
] %]
[% form.select( name = 'type', items = layer_types ) %]

It's also possible to just use an array given to the template by the handler.

Radio

Here is an example of a radio form input in a legend fieldset with two radio buttons:

<fieldset>
<legend><span>[% ".label.post_as" | ml %]</span></legend>
[%- form.hidden( id = "poster_remote", name = "poster_remote", value = remote.user ) -%]
<ul><li>[%-
    form.radio( label = remote.user
        name = "post_as"
        id = "post_as_remote"

        value = "remote"
        default = ( post_as == "remote" )
    ) -%]</li>
    <li>[%- post_as_other_label = ".label.post_as_other" | ml;
    form.radio( label = post_as_other_label
        name = "post_as"
        id = "post_as_other"

        value = "other"
        default = ( post_as == "other" )
    ) -%]</li></ul>
</fieldset>

Submit

[% form.submit( name = "action:create", value = dw.ml( '.btn.layoutspecific.create' ) ) %]