Problem with checbox in add/edit.php always stay "true"

Permalink
I want to create custom "checkbox" - for this example named "test" (boolean).
In db.xml:
<field name="test" type="boolean">
      <default value="0"/>
 </field>

In form.php (add/edit):
<div class="checkbox">
<label>
<input type="checkbox" name="test" value="1" <?php if ($test == 1) {
   ?>checked<?php
} ?> />
<?= t('test checkbox.') ?>
</label>
</div>

In view:
echo "i am test checkbox" . $test;

When the block is added "$test" var = 0. "THE PROBLEM" once i set the checkbox to be "true" (1) - it always! stay like this (even when i un-check "test". I also check the DB table in PHPmyAdmin) - what i missing?

siton
 
hutman replied on at Permalink Best Answer Reply
hutman
In your save function you need to do something like this

public function save($args) {       
   $args['test'] = intval($args['test']);
   parent::save($args);
}


Because if you do not check the checkbox nothing comes through in the POST so the value never gets reset to 0.
siton replied on at Permalink Reply
siton
Thanks!! :) I add this code to the controller and now its works fine :)