It looks like you're new here. If you want to get involved, click one of these buttons!
Hi,
I'm stuck with creating a boolean true/false toggle. Since I have to work with postgre it adds a twist, but I try my best to describe my problem.
Boolean fields in postgre return as (string) "t"
for TRUE and (string) "f"
for FALSE.
To make it more compatible in the future (in case I'm switching to mysql), I thought I use a pre_process
function to return a correct bool variable, no matter which format.
in my_helper.php
/**
* convert string, into boolean
*
* useful as filter for postgres bool fields
* @param mixed $val string or in or boolean to be converted "t", "f", true, false, 'true', 'false','yes','no',1,0,'1','0' to boolean
* @return [type] [description]
*/
function boolean_val($val)
{
if($val === "t" || $val === "f"){
$val = (bool) ($val === "t");
}
return filter_var($val, FILTER_VALIDATE_BOOLEAN);
}
The best case would be a checkbox to toggle true/false - but I'm out of luck with all the ways I approached.
Generally, if I set the checkbox or the radio buttons to true/false, it's getting written into the database (checkbox, only if checked), but the state is not visible, after the page gets reloaded)
I set radio button to 'yes' in the example below and after sending the form, the radio button is set to "no/false" even if True in the database.
<?php
require_once(FUEL_PATH.'models/Base_module_model.php'); // Fuel Way
class Praemien_model extends Base_module_model { // Fuel Way
protected $dsn = 'legacy';
protected $key_field = 'pr_id';
public $boolean_fields = ['pr_freigegeben'];
function __construct()
{
parent::__construct('praemien');
}
function form_fields($values = array(), $related = array())
{
$fields = parent::form_fields($values, $related);
$fields = array_replace_recursive($fields, array(
'pr_freigegeben' => [
'label' => 'Freigabe',
'type' => 'boolean', //comes also from the DB Schema
'mode' => 'radios',
'options' => ['no','yes'],
#'pre_process' => 'boolean_val',
#'post_process' => 'boolean_val',
#'checked' => true
#'mode' => 'radios'
],
));
return $fields;
}
}
I hope you were able to follow me.
I tried to debug create_enum()
, but I got lost and could not figure out where the database value is coming from.
Any help/hint is appreciated.
Thanks.
Comments
What if you set the type to "select" and change the option parameters to the following:
'type' => 'select'
works as expected and writes the correct values to DB.It would be great to emulate somehow the yes/no ENUM behaviour that FUEL CMS uses (to create toggles in the listing etc) and to use checkboxes since that communicates the state better.
This works as well and renders two radio fields
This works as well
I assume, the boolean field is not really made for this case, even if I remember, reading somewhere here, that a checkbox is possible too. (here it is: https://forum.getfuelcms.com/discussion/comment/6485/#Comment_6485)
Edit: I believe the on_before_save() is the way to go. I'm going to report back.
Ok, i found the culprit with the checkbox (not showing checked if the value is true)
In Form_builder.php
set_field_values($values)
there is a condition, checking if a checkbox value is true/1/y/yes and as stated in my initial posting, postgres true = (string) t.I expected my pre_process function (boolean_val) on the field, would turn it into the correct value, but it is executed much later.
Is there a hook before
set_field_values
where I could perform the conversion or do I have to change the code block in Form_builder.php?What would be the best way to solve it without changing fuel cms source files?
There is an "edit_method" module parameter you can specify which is the method on your model to use for retrieving information. You could create your own method or overwrite your model's find_one_array() method to return the data in the format you need. By default it is the find_one_array (it should return an array of arrays and not objects).
http://docs.getfuelcms.com/modules/simple