Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Selected" not set for select_field #2226

Open
brianread108 opened this issue Jan 23, 2025 · 3 comments
Open

"Selected" not set for select_field #2226

brianread108 opened this issue Jan 23, 2025 · 3 comments

Comments

@brianread108
Copy link

Can someone explain why the hookScript field gets its default "selected" set according to the value indicated whereas the status field does not get a "selected" set,

<p><span class=label>
		%=l('lets_SERVICE_STATUS')
	</span><span class=data>
		% my @status_options = [['Disabled' => 'disabled'], ['Enabled' => 'enabled'], ['TEST' => 'test']];
		% param 'status' => $lets_data->{status} unless param 'status';
		%= select_field 'status' => @status_options, class => 'input'
	<br></span>	</p>
	<p><span class=label>
		%=l('lets_HOOKSCRIPT_STATUS')
	</span><span class=data>
		% my @hookScript_options = [['Disabled' => 'disabled'], ['Enabled' => 'enabled']];
		% param 'hookScript' => $lets_data->{hookScript} unless param 'hookScript';
		%= select_field 'hookScript' => @hookScript_options, class => 'input'
	<br></span>	</p>

{
"hookScript" => "enabled",
.....
"status" => "test",
}

Image

@brianread108
Copy link
Author

<select class="input" name="status"><option value="disabled">Disabled</option><option value="enabled">Enabled</option><option value="test">TEST</option></select>

<select class="input" name="hookScript"><option value="disabled">Disabled</option><option selected="" value="enabled">Enabled</option></select>```

@unnilennium
Copy link

disclaimer, we both work on same project Brian Read and I : SME Server.

from reading more the documentation, what was not obvious is that param is not a good choice to use while it is the one suggested in the doc https://docs.mojolicious.org/Mojolicious/Plugin/TagHelpers .

from Mojolicious/Controller#param
Access route placeholder values that are not reserved stash values, file uploads as well as GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body, in that order. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use "every_param". Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by default.

from Mojolicious/Controller#stash
Non-persistent data storage and exchange for the current request, application wide default values can be set with "defaults" in Mojolicious. Some stash values have a special meaning and are reserved, the full list is currently action, app, cb, controller, data, extends, format, handler, inline, json, layout, namespace, path, status, template, text and variant. Note that all stash values with a mojo. prefix are reserved for internal use*.
from my experience because of those path, status, template, text we should not use param !
and make a function to add selected.

while mojo.* stash values seem not to cause any conflict with any project trying to use the helper, some of the mentioned reserved terms are one we could expect to occur quite frequently in any project using Mojolicious.... status, path , app, data, format, layout,template, text ... are among them. so the doc is showing something that is likely not to work, whilenot directly stated on the helper documentation it might fail :
% param status => 'enabled' unless param 'status';

My guess is that changing the behaviour of param is probably not something without risk, but probably providing an alternative as something part of the helper TagHelpers could be reasonable ?

my code could be improved by someone else, but this could be an approach:

     $self->helper( selected_field => sub {
                    my $self = shift;
                    my @options = shift;
                    my $selected = shift;
                    my $count = 0;
                    # search for occurrence of value $selected in arrays; if found add selected => 'selected'
                    for  (my $i = 0; $i <= $#{$options[0]} ; $i++){
                      if (grep /^$selected$/,  @{$options[0][$i]}) {
                        push( @{$options[0][$i]} ,'selected', 'selected' );
                        $count++;last;
                      }
                    }
                    push ( @{$options[0]} ,[ ucfirst( $selected), $selected, 'selected', 'selected'] ) if ($count <1);
                    return @options;
    });

and in template:

                <p><span class=label>
                        %=l('lets_SERVICE_STATUS')
                </span><span class=data>
                        % my @status_options = selected_field([['Disabled' => 'disabled'], ['Enabled' => 'enabled'], ['TEST' => 'test']], $lets_data->{status});
                        %= select_field 'status' => @status_options, class => 'input'
                <br></span>     </p>

also could need some more helpers for other type of field like :

@guest20
Copy link

guest20 commented Feb 15, 2025

%= select_field 'status' => @status_options, class => 'input'

... this looks like it's missing a \ from before the @ ... it might just be from pasting into github though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants