1

I want create a checkbox group and need to check some checkboxes.

$user is a list of all "Users". The method $activity->users(u_id) check if the user is "active" and return 1/0. But it doesn't matter what it return, the checkbox is always checked if I use this code:

% $user->foreach( sub {
% my $checked = $activity->users($_[0]->u_id) ? 'checked' : '';
 <span><%= check_box 'users' => $_[0]->u_id, checked => $checked  %>
 %= $_[0] 
 %= $checked
 </span>
 % });

The documentation unfortunatly doesn't explain how it is right and I also didn't found any other site which explain how it should be.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Struppi
  • 153
  • 5
  • Does it work if you do `my $checked = $activity->users($_[0]->u_id) ? 'checked' : undef` instead of `my $checked = $activity->users($_[0]->u_id) ? 'checked' : ''`? – Dada Aug 10 '22 at 09:30

1 Answers1

3

In HTML, as long as a checkbox (by "checkbox" I mean "input with checkbox type") has a checked attribute, the checkbox will be checked, regardless of the value of the checked attribute.

So, if you don't want the checkbox to be checked, you should create it without a checked attribute. For instance:

% if ($activity->users($_[0]->u_id)) {
  <span><%= check_box 'users' => $_[0]->u_id, checked => 1  %>
% } else {
  <span><%= check_box 'users' => $_[0]->u_id %>
% }

Or, more concise and with less duplication, but a bit less readable in my opinion:

% my $checked = $activity->users($_[0]->u_id);
<span><%= check_box 'users' => $_[0]->u_id, ($checked ? ('checked' => 1) : ()) %>
Dada
  • 6,313
  • 7
  • 24
  • 43
  • yes, that works. I was bit curios, because the doc sounds like it should work with checked => undef and this would be readable. – Struppi Aug 10 '22 at 08:55
  • @Struppi but you were using an empty string `''` and not `undef`. – simbabque Aug 10 '22 at 09:19
  • @simbabque are you sure that it would work with `undef`? I considered that as well, but after checking `TagHelpers` source code, I'm not sure that it would work better with `undef`... (I can't test this right now) – Dada Aug 10 '22 at 09:22
  • No, I'm not. TBH I don't quite understand what `_input` does with `checked`. – simbabque Aug 10 '22 at 09:23
  • @simbabque Yeah I'm not sure either what it does, but I think that `if (my @values = @{$c->every_param($name)}) {` shouldn't be entered, and that `checked => undef` should thus be passes as-is to `_validation`. I haven't followed all the callees of `_validation`, but I would assume that none of them would remove `checked => undef`... But that's worth checking. I added a comment to the question asking OP to try it. – Dada Aug 10 '22 at 09:30
  • 2
    The [doc](https://docs.mojolicious.org/Mojolicious/Plugin/TagHelpers#check_box) is clear in that it renders with `checked` If `checked=>undef` is passed to the helper. – clamp Aug 10 '22 at 10:38
  • @clamp Good point, thanks – Dada Aug 10 '22 at 12:32
  • I did tried it of course. No, it don't work – Struppi Aug 13 '22 at 21:24