Undefined index (input text name) in Back office form customization (Prestashop)

假如想象 提交于 2020-07-22 08:21:58

问题


I'm creating a Prestashop 1.5 module, In the module Back office customization i got this error behind the input text error in module back office configuration Notice: Undefined index: name in /var/www/presta/cache/smarty/compile/7d2a4e588611a8a1dd049f82d14a4ae0b20fe990.file.form.tpl.php on line 228

line 228 is like this

<?php $_smarty_tpl->tpl_vars['value_text'] = new Smarty_variable($_smarty_tpl->tpl_vars['fields_value']->value[$_smarty_tpl->tpl_vars['input']->value['name']], null, 0);?>

It lookes like it's related with input text value.

name is the input text name. I don't know how to fix this problem

my displayForm function code is here

public function displayForm()
{

$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 $id_lang = (int)Context::getContext()->language->id;

$categories = $this->getCMSCategories(false, 0, (int)$id_lang);
$options = array();

foreach ($categories as $category)
{

    $pages = $this->getCMSPages((int)$category['id_cms_category'], false, (int)$id_lang);

    foreach ($pages as $page){
    //  var_dump($page['meta_title']);exit;
        $radioelement =array(
                'id' => 'id_cms'.(int)$page['id_cms'],
                'value' => (int)$page['id_cms'],
                'label' => $page['meta_title']
        );
    $options[]  = $radioelement;
    }   
}


//$spacer = str_repeat('&nbsp;', $this->spacer_size * (int)$depth);

$fields_form[0]['form'] = array(
        'legend' => array(
                'title' => $this->l('Settings'),
        ),

        'input' => array(
                array(
'type'     => 'text',                                // This is a regular <input> tag.
'label'    => $this->l('Name'),                      // The <label> for this <input> tag.
'name'     => 'name',                                // The content of the 'id' attribute of the <input> tag.
'size'     => 50,                                    // The content of the 'size' attribute of the <input> tag.
'required' => true,                                  // If set to true, this option must be set.
'desc'     => $this->l('Please enter your name.')    // A help text, displayed right next to the <input> tag.
 ),
                 array(
                'type' => 'radio',
                'label' => $this->l('choose a cms page'),
                'name' => 'id_cms',
                'class' => 't',
                 'values' => $options,
                'is_bool' => false,
                'required' => true
        )
        ),

        'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'button',
                'name'=>'submitCmsReadMore'
        )
);

$helper = new HelperForm();

// Module, t    oken and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;

// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true;        // false -> remove toolbar
$helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submitCmsReadMore';
$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['MOD_CMS_READ_MORE_ID_CMS'] = Configuration::get('MOD_CMS_READ_MORE_ID_CMS');
$helper->fields_value['MOD_CMS_READ_MORE_NB_CHARS'] = Configuration::get('MOD_CMS_READ_MORE_NB_CHARS');


return $helper->generateForm($fields_form);
}

Thank you in advance.


回答1:


I think you need to add default values for forms inputs:

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

    $helper->fields_value['name'] = 'nombre'; //If you want an empty value simply use ''
    $helper->fields_value['id_cms'] = 'value3';

I'm making my first module and use switch sentence to know if I'm creating new values or modifying an existing value:

switch($action) {

  case 'new':
    $helper->fields_value['name'] = ''; //Empty input
    $helper->fields_value['id_cms'] = 'value1'; //Default value selected
    break;

  case 'edit':
    $data = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'table` WHERE id = '.$_POST["id"].''); //Recover from data from database using id passed trough $_POST["id"].
    $helper->fields_value['name'] = $data["name"]; //Name from database
    $helper->fields_value['id_cms'] = $data["id_cms"]; //Radio value from database
    break;
}

$action variable came from previous form passed trough $_POST["action"] in a previous "edit" submit button. and $id for example came from an hidden input un the previous form.




回答2:


I solved the problem i changed this code

$helper->fields_value['name'] = Configuration::get('MOD_CMS_READ_MORE_NB_CHARS');
$helper->fields_value['id_cms'] =  Configuration::get('MOD_CMS_READ_MORE_ID_CMS');


来源:https://stackoverflow.com/questions/21530041/undefined-index-input-text-name-in-back-office-form-customization-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!