Jump to content

Adding Form field dynamically in helper form Prestashop 1.6


shobhitverma

Recommended Posts

I have a back office admin controller in which I am using the helper form in renderform() to generate a form and it is working form but I want a functionality to add more field(If user wants to add a new field like add more services on click then create 2 filed one for sevice name and another one for Service price) in that form dynamically.

  • Like 1
Link to comment
Share on other sites

I have done this by helper list, while rendering the form I am returning a render list also, check renderAdditionalOptionsList() usage

public function renderForm() {
    $id_citydelivery = (int) Tools::getValue('id_citydelivery');
    $citydeliveryObj = new CityPortModel($id_citydelivery);
    if (Validate::isLoadedObject($citydeliveryObj)) {
        $_legends = $citydeliveryObj->legend;
        $_legend_array = explode(',', $_legends);
        foreach ($_legend_array as $_legend) {
            $this->fields_value['legend_' . $_legend] = $_legend;
        }
    }
    $temp = $this->fields_value;
    $fields_form_1 = array(
        'form' => array(
            'legend' => array('title' => $this->l('Price and Delivery Destination Manager'), 'icon' => 'icon-cogs'),
            'input' => array(
                array(
                    'type' => 'select',
                    'label' => $this->l('Country'),
                    'name' => 'id_country',
                    'required' => false,
                    'lang' => false,
                    'options' => array(
                        'query' => $this->getCountries(),
                        'id' => 'value',
                        'name' => 'name'
                    )),
                array(
                    'type' => 'text',
                    'label' => $this->l('Portname'),
                    'name' => 'portname',
                    'size' => 255),
                array(
                    'type' => 'hidden',
                    'name' => 'id_citydelivery'
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Per m3'),
                    'name' => 'perm3',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Service charges'),
                    'name' => 'servicecharges',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Insurance'),
                    'name' => 'insurance',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Inspection'),
                    'name' => 'inspection',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Certificate'),
                    'name' => 'certificate',
                    'size' => 255),
                array(
                    'type' => 'checkbox',
                    'label' => $this->l('Service legend'),
                    'name' => 'legend',
                    'required' => false,
                    'lang' => false,
                    'values' => array(
                        'query' => $this->getLegends(),
                        'id' => 'value',
                        'name' => 'name'
                    )),
                array(
                    'type' => 'switch',
                    'label' => $this->l('Active'),
                    'name' => 'active',
                    'required' => false,
                    'is_bool' => true,
                    'values' => array(array(
                            'id' => 'active_on',
                            'value' => 1,
                            'label' => $this->l('Active')), array(
                            'id' => 'active_off',
                            'value' => 0,
                            'label' => $this->l('Inactive')))),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'type' => 'submit'
            ),
            'buttons' => array(
                array(
                    'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite($this->name),
                    'title' => $this->l('Cancle'),
                    'icon' => 'process-icon-cancel'
                )
            )
        ),
    );
    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->toolbar_scroll = true;
    $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $this->fields_form = array();
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitAddcitydelivery';
    $helper->currentIndex = AdminController::$currentIndex;
    $helper->token = Tools::getAdminTokenLite($this->name);
    $helper->tpl_vars = array(
        'fields_value' => $this->getFormValues($citydeliveryObj, $temp),
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id
    );
    $_1 = $helper->generateForm(array($fields_form_1));
    $return = $_1;
    if ($this->display != 'add') {
        $return .= $this->renderAdditionalOptionsList($id_citydelivery);
    }
    return $return;
}

public function initPageHeaderToolbar() {
    if ($this->display == 'edit' || $this->display == 'add') {
        $this->page_header_toolbar_btn['back_to_list'] = array(
            'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
            'desc' => $this->l('Back to list', null, null, false),
            'icon' => 'process-icon-back'
        );
    }
    parent::initPageHeaderToolbar();
}

function getFormValues($citydeliveryObj, $temp) {
    $arrTwo = array(
        'id_citydelivery' => Tools::getValue('id_citydelivery', $citydeliveryObj->id_citydelivery),
        'id_country' => Tools::getValue('id_country', $citydeliveryObj->id_country),
        'portname' => Tools::getValue('portname', $citydeliveryObj->portname),
        'perm3' => Tools::getValue('perm3', $citydeliveryObj->perm3),
        'servicecharges' => Tools::getValue('servicecharges', $citydeliveryObj->servicecharges),
        'insurance' => Tools::getValue('insurance', $citydeliveryObj->insurance),
        'inspection' => Tools::getValue('inspection', $citydeliveryObj->inspection),
        'certificate' => Tools::getValue('certificate', $citydeliveryObj->certificate),
        'active' => Tools::getValue('active', $citydeliveryObj->active),
    );
    return array_merge($temp, $arrTwo);
}

protected function renderAdditionalOptionsList($id_citydelivery) {
    $this->fields_list = array(
        'service_name' => array(
            'width' => 'auto',
            'orderby' => false,
            'title' => $this->l('Service Name'),
            'type' => 'text',
            'search' => false,
        ),
        'service_desc' => array(
            'type' => 'text',
            'title' => $this->l('Service Description'),
            'search' => false,
            'orderby' => false,
        ),
        'charge' => array(
            'title' => $this->l('Service Charge'),
            'type' => 'text',
            'search' => false,
            'orderby' => false,
        ),
        'active' => array(
            'title' => $this->l('Status'),
            'active' => 'status',
            'type' => 'bool',
            'search' => false,
            'orderby' => false
        )
    );
    $linkToAdditionServiceController = Context::getContext()->link->getAdminLink('AdminAdditionalService', false);
    $helperList = new HelperList();
    $helperList->table = 'additional_service';
    $helperList->shopLinkType = '';
    $helperList->simple_header = false; //For showing add and refresh button
    $helperList->identifier = 'id_additional_service';
    $helperList->actions = array('edit', 'delete');
    $helperList->show_toolbar = false;
    $helperList->toolbar_btn['new'] = array(
        'href' => $linkToAdditionServiceController . '&addadditional_service&id_citydelivery=' . $id_citydelivery . '&token=' . Tools::getAdminTokenLite('AdminAdditionalService'),
        'desc' => $this->l('Add new')
    );
    $helperList->title = "Additional Option Manager";
    $helperList->currentIndex = $linkToAdditionServiceController;
    $helperList->token = Tools::getAdminTokenLite('AdminAdditionalService');
    $content = $this->getListContent($id_citydelivery);
    $helperList->listTotal = count($content);
    return $helperList->generateList($content, $this->fields_list);
}

Untitled.thumb.png.c20880438e2cff11904473b7660bc2c9.png

Edited by shobhitverma (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...