prestashop中的复选框值

问题描述 投票:0回答:1

我正在使用prestashop并尝试使用HelperForm从带有复选框的表单中获取值

所以我拥有的是:

$fields_form[0]['form']= [
        'legend'=> [
            'title'=> $this->l('Indexation')
        ] ,
        'input'=>[
            [
                'type'=>'text',
                'label'=> $this->l('Base(s) à indexer'),
                'name'=>'options',
                'size'=>20,
                'required'=>true
            ]
        ],
        'submit'=>[
            'title' => $this->l('Save'),
            'class' => 'btn btn-default pull-right'
        ]
    ];

然后

$helper = new HelperForm();
[...]
$helper->toolbar_btn = array(
        'save' =>
            array(
                'desc' => $this->l('Save'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
            ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );

    // Load current value
    $helper->fields_value['options'] = Configuration::get('options');

    return $helper->generateForm($fields_form);

在我的getContent我有:

$my_module_name = strval(Tools::getValue('options'));
return $my_module_name;

所以直到那里我没有问题。我在文本输入中写'test'然后返回'test'但是我不想要文本输入我想要一个复选框输入所以我改变了我的表单:

 $fields_form[0]['form']= [
        'legend'=> [
            'title'=> $this->l('Indexation')
        ] ,
        'input'=>[
            [
                'type'=>'checkbox',
                'label'=> $this->l('Base(s) à indexer'),
                'name'=>'options',
                'required'=>true,
                'values'=>[
                    'query'=>$options,
                    'id'=>'id',
                    'name'=>'name'
                ]
            ]
        ],
        'submit'=>[
            'title' => $this->l('Save'),
            'class' => 'btn btn-default pull-right'
        ]
    ];

和$选项是:

$options = [
        [
            'id'=>1,
            'name'=>'test'
        ],
        [
            'id'=>2,
            'name'=>'test2'
        ]
    ];

在我的getContent()return (Tools::getValue('options'));但是,没有显示任何内容。

此外,如果我做return sizeof(Tools::getValue('options))它给我1无论我检查复选框

checkbox prestashop-1.6
1个回答
1
投票

首先,您需要使用[]设置字段的名称

$fields_form[0]['form']= [
    'legend'=> [
        'title'=> $this->l('Indexation')
    ] ,
    'input'=>[
        [
            'type'=>'checkbox',
            'label'=> $this->l('Base(s) à indexer'),
            'name'=>'options[]',
            'required'=>true,
            'values'=>[
                'query'=>$options,
                'id'=>'id',
                'name'=>'name'
            ]
        ]
    ],
    'submit'=>[
        'title' => $this->l('Save'),
        'class' => 'btn btn-default pull-right'
    ]
];

然后,您的选项应该有一个值:

$options = [
    [
        'id'=>1,
        'name'=>'test',
        'val' => 1
    ],
    [
        'id'=>2,
        'name'=>'test2',
        'val' => 2
    ]
];

然后您可以通过以下方式获取选中的值:

Tools::getValue('options')

编辑:在1.6中我们有帮助管理员tpl:

{foreach $input.values.query as $value}
    {assign var=id_checkbox value=$input.name|cat:'_'|cat:$value[$input.values.id]}
    <div class="checkbox{if isset($input.expand) && strtolower($input.expand.default) == 'show'} hidden{/if}">
        {strip}
            <label for="{$id_checkbox}">
                <input type="checkbox" name="{$id_checkbox}" id="{$id_checkbox}" class="{if isset($input.class)}{$input.class}{/if}"{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if} />
                {$value[$input.values.name]}
            </label>
        {/strip}
    </div>
{/foreach}

因此,要设置要返回的复选框值,我们需要传递val:

{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}

另外,要在加载页面时进行检查,我们会传递值以符合条件:

{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if}
© www.soinside.com 2019 - 2024. All rights reserved.