How to create checkbox in add block pop up window

Permalink 1 user found helpful
Hi,

I create a block which requires a checkbox in the add block pop up window.

Here is part of my code in the form_setup_html.php
<?php
echo '<div class="ccm-block-field-group">';
echo $form->label('previewMode', t('Enable normal user preview mode:'));
echo $form->checkbox('previewMode', 1, true);
echo '</div>';
?>


Here is part of my code in the db.xml
<field name="previewMode" type="I"><unsigned /></field>


But even if I uncheck the checkbox, the value stored in the database is still 1, rather than null which I expected.

And I need the value of checkbox to do some check in the view.php.

Thank you very much :)

 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Checkboxes are annoying because in HTML forms, if the box is unchecked then *nothing* is posted (note the difference between posting the value of false or 0 versus not posting anything at all).
To work around this, you need to add some code to your block's save() method like so:
public function save($args) {
  $args['previewMode'] = empty($args['previewMode']) ? 0 : 1;
  parent::save($args);
}
sfeng replied on at Permalink Reply
Hi jordanlev,

Thank you so much :)
maggiew replied on at Permalink Reply
Hi there,

I'm really struggling with getting checkboxes to work correctly in my blocks, and am hoping someone might be able to point out what I'm doing wrong (have tried various solutions/fixes posted across different threads, with no luck...).

I have the following in my db.xml file:

<field name="coupon" type="I1">
      <unsigned />
      <notnull />
      <default value="0" />
    </field>


In both add.php and edit.php I have:

defined('C5_EXECUTE') or die(_("Access Denied."));
   $this->inc('form_setup_html.php');


And in form_setup_html.php I have:

echo $form->checkbox('coupon', '1', $coupon);


Finally, in controller.php I've added:

public function save($args) {
      $args['coupon'] = isset($args['coupon']) ? 1 : 0;
      parent::save($args);
   }


When I'm in the block's add/edit view, the checkbox shows up (unchecked by default). But if I check the box, save, and then go back in to edit the block, the box is unchecked. I'm not sure if the value's just not saving, or if I'm retrieving it incorrectly in the view. Any pointers would be much appreciated.

Thanks,
Maggie
jordanlev replied on at Permalink Reply
jordanlev
It looks like your code is correct. Try checking the block's database table (using phoMyAdmin, for example) after saving and see if the proper value is being saved. This will help track down the problem to either it not saving properly or not displaying properly.
My guess is there's some other code you're not showing that is interfering some how.
You might want to ZIP up your whole block and post it here.
maggiew replied on at Permalink Reply
Ok - I think I know what's wrong. After your suggestion to check if the value was being saved in the block's db table, I realized that column wasn't there at all. I had added it to db.xml, but I guess the database needs to be refreshed for it to 'take'. I'm having a separate problem getting that to work from within the Concrete5 dashboard (getting 'Fatal error: Call to undefined method Controller::refresh_schema() in /Users/Maggie/Sites/concrete/concrete/controllers/dashboard/settings/controller.php on line 194'), so for now I've manually altered the table using

ALTER TABLE btSrQuoteForm ADD COLUMN `coupon` tinyint(3) unsigned NOT NULL DEFAULT 0;


Thanks,
Maggie