select_field tag helper - how to add 'selected'?

469 views
Skip to first unread message

Charlie Brady

unread,
Feb 6, 2013, 1:29:31 PM2/6/13
to mojol...@googlegroups.com

Documentation for the tag helpers is "by example only", and doesn't define
valid syntax.

http://mojolicio.us/perldoc/Mojolicious/Plugin/TagHelpers#select_field

How do I set the boolean "selected", viz:

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>

Glen

unread,
Feb 6, 2013, 1:55:15 PM2/6/13
to mojol...@googlegroups.com
Set the parameter to what you want selected:

param(name => 'audi');
> --
> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
> To post to this group, send email to mojol...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mojolicious?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Charlie Brady

unread,
Feb 6, 2013, 1:50:06 PM2/6/13
to mojol...@googlegroups.com


On Wed, 6 Feb 2013, Glen wrote:

> Set the parameter to what you want selected:
>
> param(name => 'audi');

Thanks. I see this in the documentation:

Most form helpers can automatically pick up previous input values and
will show them as default. You can also use "param" in
Mojolicious::Plugin::DefaultHelpers to set them manually and let
necessary attributes always be generated automatically.

I found another way too - set selected attribute with a dummy value.

select_field country => [[Volvo => 'volvo'], [Saab => 'saab'], [VW => 'vw'],[Audi => 'audi', selected => 'true']]

Charlie Brady

unread,
Mar 28, 2017, 2:16:51 PM3/28/17
to mojol...@googlegroups.com

On Wed, 6 Feb 2013, Glen wrote:

> Set the parameter to what you want selected:
>
> param(name => 'audi');

I've discovered that there are some parameter names when that is not true.
I think this might be a case of reserved words in the stash.

% param status => 'enabled';
%= select_field status => [['DISABLED' => 'disabled'], ['ENABLED' =>
'enabled']]

does not render

<select name="status1"><option value="disabled">DISABLED</option><option
selected value="enabled">ENABLED</option></select>

as expected, but rather as:

<select name="status"><option value="disabled">DISABLED</option><option
value="enabled">ENABLED</option></select>

Changing the field name to something other than 'status' (or 'text',
'json', 'inline', etc) makes it behave as expect..

The full list of reserved stash values is listed here:

http://mojolicious.org/perldoc/Mojolicious/Controller#stash

This is a real trap for the unwary. Should ->param() warn about use of
reserved words?
Reply all
Reply to author
Forward
0 new messages