Jump to content

Validate form in a module


Recommended Posts

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

  • 2 weeks later...

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

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

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...