⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Boolean Fields



steven1350

steven1350
  • profile picture
  • Member

Posted 04 April 2012 - 20:10 PM

Is there such a feature in Grocery Crud that allows for boolean fields (something like a yes/no field) giving the user a checkbox or similar?

I have looked over the examples, and have not seen something that allows this. I am sure I am not the only person who would appreciate a feature like that.

Thanks

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 04 April 2012 - 21:28 PM

Yes it has. So if you have a tinyint or int with length 1 (mysql don't have a real boolean, even the boolean one has length in memory as a tinyint) it automatically do it as a "active" and "inactive" radio buttons. If it doesn't work automatically for you, you can always change the type of the field with the change_field_type . You can see an example at: http://www.grocerycr...ange_field_type the type of the "active" (1) or "inactive" (0) is the true_false. So you can have:


$crud->change_field_type('has_categories', 'true_false');


and you will have this result --> [attachment=92:status.png]

If you want to change the "active" or "inactive" string (for example "yes" or "no")it is hardcoded at grocery CRUD library ( application/libraries/grocery_crud.php at line 1219 )


....
protected $default_true_false_text = array('inactive' , 'active');
....


so for now just change this at the grocery_crud library

gab

gab
  • profile picture
  • Member

Posted 18 May 2012 - 13:57 PM

Hi all

Just for info, after i read this topic, for french users i have changed this :

protected $default_true_false_text  = array('non' , 'oui');


it didn't work and got me mad :)

Now, since v1.2.1 we have to change it in assets/grocery_crud/languages/(mylang).php...

No longer need to modify it in the library.

HumbleMonk

HumbleMonk
  • profile picture
  • Member

Posted 27 September 2012 - 04:29 AM

Is there any way to add multiple different custom strings for the boolean values?

e.g. I want to have one boolean field with values ("yes", "no") and another one with values ("available", "unavailable"). Is there any way to achieve this?

Changing the default boolean string only allows for one custom value.

rothkj1022

rothkj1022
  • profile picture
  • Member

Posted 20 February 2013 - 18:25 PM

I second HumbleMonk's suggestion of having different label options.  Also, an option to display the field as a single checkbox instead of radio buttons would be nice.


davidoster

davidoster
  • profile picture
  • Member

Posted 20 February 2013 - 22:38 PM

Use the field_type to achieve what you want. Possibly the enum field type for you [member=HumbleMonk] and multiselect with one item within the array for you [member=rothkj1022].


rothkj1022

rothkj1022
  • profile picture
  • Member

Posted 14 June 2013 - 16:34 PM



Use the field_type to achieve what you want. Possibly the enum field type for you [member=HumbleMonk] and multiselect with one item within the array for you [member=rothkj1022].

 

I can see how setting your own enums using field_type is good for changing the label options.  That's all good and I think solves the multiple label options issue.

 

The other issue of having a simple boolean checkbox isn't covered by what you suggested, though.  For some boolean values I think it would be more user-friendly to offer just a single checkbox (with no value displayed), instead of 2 radio buttons with yes and no values.  If the checkbox is checked, value is true.  If not checked, false.


davidoster

davidoster
  • profile picture
  • Member

Posted 15 June 2013 - 08:11 AM

The other issue of having a simple boolean checkbox isn't covered by what you suggested, though.  For some boolean values I think it would be more user-friendly to offer just a single checkbox (with no value displayed), instead of 2 radio buttons with yes and no values.  If the checkbox is checked, value is true.  If not checked, false.

 

 This is true what you point out. At least at the moment there isn't such a functionality of a checkbox element.


Ruben Vanhoeyveld

Ruben Vanhoeyveld
  • profile picture
  • Member

Posted 21 November 2013 - 18:24 PM

I'm looking for some sort of a callback_read_field() functionality, so that true and false are not just shown as "1" or "" (nothing for 0). If I could just put this in a callback on the read field, I could change 1 to Yes and "else" to No...

 

Or is there another way to achieve this?


Adriano Gonçalves

Adriano Gonçalves
  • profile picture
  • Member

Posted 11 December 2013 - 03:21 AM

I did it this way:

            $crud->field_type('active', 'dropdown', array('1' => 'Yes', '0' => 'No'));

Adriano Gonçalves

Adriano Gonçalves
  • profile picture
  • Member

Posted 17 July 2014 - 14:29 PM

I'm looking for some sort of a callback_read_field() functionality, so that true and false are not just shown as "1" or "" (nothing for 0). If I could just put this in a callback on the read field, I could change 1 to Yes and "else" to No...

 

Or is there another way to achieve this?

 

You can use callback_field().


Adriano Gonçalves

Adriano Gonçalves
  • profile picture
  • Member

Posted 17 July 2014 - 15:00 PM

That was the way that really worked for me (no changes necessary on core):

$crud->field_type('utiliza_epi', 'true_false', array('No', 'Yes'));

larasmith

larasmith
  • profile picture
  • Member

Posted 19 July 2014 - 11:18 AM

Thanks to this... :) I'm always looking for solutions that doesn't involve touching the original code of groceryCRUD... thanks!


larasmith

larasmith
  • profile picture
  • Member

Posted 01 August 2014 - 16:01 PM

I just noticed that in read() mode (displayed when view button is clicked) when the value is '0' or 'false' nothing is displayed. So I manage to get a solution by:

1.)  Creating a table called "tblBool" with this structure: 

d0dcbd9edaa1d0c9cd5ba98086cceaf6.png

2.) Set relation to this table so that the field that needs the "YES or NO" value will look upon it:

Code:

// Sets dropdown list (Reviewed bool)
$crud->set_relation('qb_reviewed_id','tblbool','bool_value');

Output (This is what it looks like during add / edit):

a1cb5cc951824b4cc2b5e7712fbd1e2b.png

Output (this is what it looks like upon clicking the view button /read() mode):

730b017148c67fda2b091f5c976a7e5f.png

 

Problem solved  ;). I just hope i helped someone with the same issues! Happy GCing!  :D 


noplan

noplan
  • profile picture
  • Member

Posted 23 January 2019 - 18:05 PM

Thank you larasmith,

unfortunately the picture files don't exist anymore. Could you upload them again, please? Thank you.

 

Cheers