Jump to content

form Switch not working


Matt_

Recommended Posts

I have this in my module php:

 

...

               array(
                        'type' => 'switch',
                        'lang' => true,
                        'label' => $this->l('My Switch'),
                        'name' => 'MODULE_NAME_switch',
                        'id' => $this->name.'_myswitch',
                        'desc' => $this->l('Description goes here.'),
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => 1,
                                'label' => $this->l('Enabled')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => 0,
                                'label' => $this->l('Disabled')
                            )
                        )
                    ),
...

But the switch doesn't change on Save.  Also the YES (default) isn't solid white (or whatever), instead it's grey.

Other reference in same function:

 

    public function install()
    {
        if (!parent::install() ||
            !$this->registerHook('somewhere') ||
            !$this->registerHook('actionObjectLanguageAddAfter') ||
            !$this->installFixtures() ||
            !$this->disableDevice(Context::DEVICE_MOBILE) ||
            !Configuration::updateValue('MODULE_NAME_switch', 0)
        )
...

            $this->smarty->assign(array(
                'mySwitch' => Configuration::get('MODULE_NAME_switch', $this->context->language->id),
...

                $values['MODULE_NAME_switch'][$lang['id_lang']] = Tools::getValue('MODULE_NAME_switch_'.$lang['id_lang']);
...

            Configuration::updateValue('MODULE_NAME_switch', $values['MODULE_NAME_switch']);
...

            $fields['MODULE_NAME_switch'][$lang['id_lang']] = Tools::getValue('MODULE_NAME_switch_'.$lang['id_lang'], Configuration::get('MODULE_NAME_switch', $lang['id_lang']));
...

How can I correct this and make it work?

Link to comment
Share on other sites

  • 2 months later...
  • 3 months later...

Hi Matt_,

 

I don't know if you found any solution for your problem but i'm new on prestashop and i now encounter the same problem.

 

I wanted to add custom fields for categories, and the text custom field works perfectly but my switch custom field saves in database but not displaynig on category back office page not like text custom field.

 

I searched a lot and i found an interesting thing, in admin/themes/default/template/heleprs/form/form.tpl, i displayed that :

 

{elseif $input.type == 'switch'}

<pre>{$fields_value|@print_r}</pre>
 
Array
(
    [image] => 
    [size] => 
    [groupBox_1] => 1
    [groupBox_2] => 1
    [groupBox_3] => 1
    [is_root_category] => 
    [name] => Array
        (
            [1] => Lentilles multifocal torique
        )

    [active] => 1
    [id_parent] => 12
    [description] => Array
        (
            [1] => 
        )

    [thumb] => 
    [meta_title] => Array
        (
            [1] => 
        )

    [meta_description] => Array
        (
            [1] => 
        )

    [meta_keywords] => Array
        (
            [1] => 
        )

    [link_rewrite] => Array
        (
            [1] => lentilles-multifocal-torique
        )

    [groupBox] => 
    [description_long] => Array
        (
            [1] => 
papitoxxxx


        )

    [eyesbox] => Array
        (
            [1] => 1
        )

)

We can see that last eyebox switch custom field returns an array or classic switch input just returns a string.

 
Find this line and replace it with new condition : $fields_value[$input.name][1] == $value.value
 
<input type="radio" name="{$input.name}"{if $value.value == 1} id="{$input.name}_on"{else} id="{$input.name}_off"{/if} value="{$value.value}"{if $fields_value[$input.name] == $value.value || $fields_value[$input.name][1] == $value.value} checked="checked"{/if}{if isset($input.disabled) && $input.disabled} disabled="disabled"{/if}/>

 

It works now for me but i do not know where the problem exactly.

Edited by missill (see edit history)
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...