Stiven Gallardo Posted August 18, 2020 Share Posted August 18, 2020 Good evening people, could someone help me with something, it turns out that I am creating a module in which I am displaying a form until everything is fine, but I would like to know how I can detect the sending of data from the form. in some php file of the module that I am developing, I would be very grateful if you help me Link to comment Share on other sites More sharing options...
Minsky_ae Posted August 28, 2020 Share Posted August 28, 2020 Hi @Stiven Gallardo, I'm new to PrestaShop but maybe I can give you some hint. If you've got a renderForm() method, you normally defined your submit button name here: protected function renderForm() { $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->module = $this; $helper->default_form_language = $this->context->language->id; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); $helper->identifier = $this->identifier; $helper->submit_action = 'submit_button'; $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'fields_value' => $this->getConfigFormValues(), /* Add values for inputs */ 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id, ); return $helper->generateForm(array($this->getConfigForm())); } Then in the getContent() method, you can use the Tools::isSubmit method to check if you have to process data: public function getContent() { /** * If values have been submitted in the form, process. */ if (((bool) Tools::isSubmit('submit_button')) == true) { $this->postProcess(); } $this->context->smarty->assign('module_dir', $this->_path); $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl'); return $output . $this->renderForm(); } In your postProcess() you handle form data: protected function postProcess() { $form_values = $this->getConfigFormValues(); foreach (array_keys($form_values) as $key) { Configuration::updateValue($key, Tools::getValue($key)); } } Here an example with data saved in Configuration table: protected function getConfigFormValues() { return array( 'FIELD_ONE' => Configuration::get('FIELD_ONE', ''), 'FIELD_TWO' => Configuration::get('FIELD_TWO', false) ); } Hope it can help you a little! Link to comment Share on other sites More sharing options...
Stiven Gallardo Posted August 29, 2020 Author Share Posted August 29, 2020 10 hours ago, Minsky_ae said: Hi @Stiven Gallardo, I'm new to PrestaShop but maybe I can give you some hint. If you've got a renderForm() method, you normally defined your submit button name here: protected function renderForm() { $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->module = $this; $helper->default_form_language = $this->context->language->id; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); $helper->identifier = $this->identifier; $helper->submit_action = 'submit_button'; $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'fields_value' => $this->getConfigFormValues(), /* Add values for inputs */ 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id, ); return $helper->generateForm(array($this->getConfigForm())); } Then in the getContent() method, you can use the Tools::isSubmit method to check if you have to process data: public function getContent() { /** * If values have been submitted in the form, process. */ if (((bool) Tools::isSubmit('submit_button')) == true) { $this->postProcess(); } $this->context->smarty->assign('module_dir', $this->_path); $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl'); return $output . $this->renderForm(); } In your postProcess() you handle form data: protected function postProcess() { $form_values = $this->getConfigFormValues(); foreach (array_keys($form_values) as $key) { Configuration::updateValue($key, Tools::getValue($key)); } } Here an example with data saved in Configuration table: protected function getConfigFormValues() { return array( 'FIELD_ONE' => Configuration::get('FIELD_ONE', ''), 'FIELD_TWO' => Configuration::get('FIELD_TWO', false) ); } Hope it can help you a little! Hi Your idea seems very good, but isn't that to verify the form data in the administrative part? Link to comment Share on other sites More sharing options...
Minsky_ae Posted August 31, 2020 Share Posted August 31, 2020 Hi @Stiven Gallardo, Sooorry I didn't read your topic right, you were talkin about a form displayed on the front?! Never tried this, I'm not gonna be much help. If you didn't already did it, you might wanna check another module with a front form to see how it behave. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now