I have a template in Mojolicious used as a frontend for a SQL-database. To toggle a bool-value (yes=1/no=0) I am using an input type of checkbox.
This is the code from the template:
<input type="checkbox" name="reinigung_ja" id="reinigung_ja" value="1"
<%= $rs->reinigung_ja ? 'checked' : ''; %>
> Reinigung <br>
It works fine to both view the present state of the reinigung_ja field in the database and to toggle it from no to yes. But it fails to toggle from yes to no since no parameter is send, if the checkbox is unchecked.
My present workaround is this code in the controller:
my $fields;
foreach ($c->req->body_params->param) {
$fields->{"$_"} = $c->req->body_params->param("$_");
}
# Workaround starts here ...
if (not exists $fields->{'reinigung_ja'}) {
$fields->{'reinigung_ja'} = 0;
}
# end of workaround;
$rs->update($fields);
I wonder if there is not a better solution?