prestashop多个复选框不保存值

时间:2016-05-28 19:21:40

标签: checkbox prestashop prestashop-1.6 helpers

我无法弄清楚为什么复选框值没有使用助手保存在数据库中。

尝试从我的模块设置中保存一些客户ID:

数组:

$custs = Customer::getCustomers();
foreach ($custs as $key => $value) {
  $options[] = array(
        'id_customer' => (int)$value['id_customer'],
        'infos' => $value['firstname'].' '.$value['lastname'].' | '.$value['email']
    );
}

复选框:

'input' => array(
        array(
            'type' => 'checkbox',
            'label' => $this->l('Customers'),
            'desc' => $this->l('Select the Customers.'),
            'name' => 'MY_MODULE_CUSTOMERS',
            'values' => array(
                'query' => $options,
                'id' => 'id_customer',
                'name' => 'infos',
            ),
        ),
)

$ _POST始终为空,但适用于其他输入。任何帮助将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

你的代码是正确的,我试过了,这就是结果 http://screencast.com/t/wfsW86iJj

  1. 您必须至少点击一个复选框。
  2. 在服务器上显示数据:

        print_r($_POST);
        die();
    

答案 1 :(得分:0)

一个更好的可能是使用groupbox 但是它非常困难,看一下prestachop的controllers目录中的AdminCustomers控制器类,它有一个多选组,它使用存储在的关系表事件单场

如果您想要轻松,使用单个字段存储在数据库中,请查看完整代码和所有步骤:https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk

在开始时不要忘记添加该行:

// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['MY_MODULE_CUSTOMERS[]'] = explode(',',$obj->id_employee);

这个$ obj是从该对象转到编辑时加载的先前存储值的表示,获取多选的字段的存储值,存储为“1,3,4,6”

并在字段形式的输入辅助列表中将选择倍数定义为:

            array(
                 'type' => 'checkbox',
                'label' => $this->l('Select and employee'),
                'name' => 'MY_MODULE_CUSTOMERS[]',
                'required' => false,
                'col' => '6',
                'default_value' => (int)Tools::getValue('id_employee_tech'),
                'values' => array(
            'query' => $options,
            'id' => 'id_customer',
            'name' => 'infos',
        ),
            ),

然后再覆盖后期处理

public function postProcess()
{
    if (Tools::isSubmit('submitTallerOrden'))
    {
        $_POST['MY_MODULE_CUSTOMERS'] = implode(',', Tools::getValue('MY_MODULE_CUSTOMERS'));
    }
    parent::postProcess();
}

这使得存储在数据库中为“1,2,3”