Jump to content

How to build a single boolean 'flag' checkbox via form helpers?


Recommended Posts

Hi!

I read the official docs but I can't figure out how to build a single checkbox element from the standard helpers.

I already have the relevant boolean entity in database and I can build radios or selects as well for it, and they work.
 

But what I'd really like is to have a single checkbox to use as a boolean flag.

How can I do it?

 

Link to comment
Share on other sites

 

I think you should use "Switch" field:

array(
	'type' => 'switch',
	'label' => $this->l('Enabled'),
	'name' => 'active_slide',
	'is_bool' => true,
	'values' => array(
		array(
			'id' => 'active_on',
			'value' => 1,
			'label' => $this->l('Yes')
		),
		array(
			'id' => 'active_off',
			'value' => 0,
			'label' => $this->l('No')
		)
	),
),

 

YES! That works! Thank you! As for help for others, I report three equivalent ways to accomplish the same task (radio, select, switch). All are referred, in this example, to a boolean field added to the address DB table and named 'expo'. All the 3 solutions have been succefully tested on an override of AdminAddressController.

 

 

SELECT

        $s_options = array(
          array( 'expo' => 1, 'name' => 'Yes' ),
          array( 'expo' => 0, 'name' => 'No' )
        );
        $temp_fields[] = array(
          'type' => 'select',
          'label' => $this->l('Is Expo'),
          'name' => 'expo',
          'required' => false,
          'options' => array(
            'query' => $s_options,
            'id' => 'expo',
            'name' => 'name'
          )
        );

RADIO

        $s_options = array(
          array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
          array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
        );
        $temp_fields[] = array(
          'type' => 'radio',
          'label' => $this->l('Is Expo'),
          'name' => 'expo',
          'required' => false,
          'class' => 't',
          'is_bool' => true,
          'values' => $s_options
        );

SWITCH

        $s_options = array(
          array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
          array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
        );
        $temp_fields[] = array(
          'type' => 'switch',
          'label' => $this->l('Is Expo'),
          'name' => 'expo',
          'required' => false,
          'is_bool' => true,
          'values' => $s_options
        );

 

 

 

 

 

 

 

Edited by stratboy (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...