Is it possible to have "checkbox" in the "edit view" section of a module in dashboard?

edited June 2014 in Share
I have followed the following tutorial on creating a simple guestbook module:

http://www.sitepoint.com/getting-started-with-fuel-cms-2/

If you look at the part where we are editing a guestbook entry, the visible field is shown with 'yes' and 'no' radio buttons. Well, this is because that field is an Enum with 'yes' and 'no' values in the database. Now is it possible to have a checkbox instead of these radio buttons? I changed the type of 'visible' field to boolean in database, but didn't get a checkbox in the dashboard view for that field. How can I achieve this? Any advice would be appreciated.

Comments

  • edited 7:18PM
    In your module model, overwrite the generated field type for "visible" to be a checkbox like so:
    function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); $fields['visible'] = array('type' => 'checkbox', 'value' => 1); return $fields; }
  • edited 7:18PM
    Thank you. This will always set the checkbox value to 1 though. What about when editing a guestbook comment whose visible field is set to 0? It will be checked when shown. How can I set the "value" of checkbox according to the value saved in the database?
  • edited 7:18PM
    I'm not quite sure I follow. Checkbox values should not change (unless you are using javascript) and they only pass data in the $_POST if they are checked. In this case, if checked, the value of 1 would be passed to your boolean field in the database. If not checked, there is no value passed in the post and so the default value will show. If you are wanting a different value passed in the post, I'd recommend using an enum. Or you can use a model hook like "on_before_save" to manipulate the values to save:
    http://docs.getfuelcms.com/general/models#hooks

    You can also set in the model the following to allow you to toggle the visibility from the list view:
    public $boolean_fields = array('visible');
  • edited June 2014
    This is what I meant in more details: Suppose that I click on an item in the list view to edit it. And the visibility of this item in database is already set to 1, so I expect in the edit view the visibility chechbox be checked, to indicate its current value in database. The problem is no matter what the value of the 'visibility' field is in database for a specific record, the checkbox of that item is never chechek when editing it. Surprisingly, the above line of code in your last comment fixed this issue! After adding the following line to the model:
    public $boolean_fields = array('visible');
    the checkbox is checked for visible items and unchecked for invisible ones when editing them. How did this line fix the problem?
  • edited 7:18PM
    Because checkboxes will not send anything over in the $_POST if unchecked, FUEL needs to check the model to see if a boolean field has been identified, and if so, will treat no $_POST variable value as simply a $_POST['visible'] = 0. It does this around line 1404 in the fuel/modules/fuel/controllers/module.php file.
Sign In or Register to comment.