Jump to content

How to use the HelperOptions class to configure a module?


nozzula

Recommended Posts

Hi,
I do not figured out how to use the HelperOptions class to configure a module.
In my module main class I've declared the following functions,

but when I click on save button, the die function return "test=###"
So the Tools::getValue() doesn't return any value

What's wrong with it?

	public function getContent()
	{
		if (Tools::isSubmit('save'.$this->name))
		{
			$id_PS_MYMODULE_OPTION1 = Tools::getValue('id_PS_MYMODULE_OPTION1');
                        $id_PS_MYMODULE_OPTION2 = Tools::getValue('id_PS_MYMODULE_OPTION2');
                        die('test=#' . $id_PS_MYMODULE_OPTION1 . '#' . $id_PS_MYMODULE_OPTION2 . '#');
			Configuration::updateValue('PS_MYMODULE_OPTION1', $id_PS_MYMODULE_OPTION1);
                        Configuration::updateValue('PS_MYMODULE_OPTION2', $id_PS_MYMODULE_OPTION2);
		}
		return $this->displayForm();
	}

	public function displayForm()
	{
		$fields_options = array(
			'general' => array(
				'title' => $this->l('Parameters'),
				'fields' => array(
                        'PS_MYMODULE_OPTION1' => array(
                            'title' => $this->l('Choose one'),
                            'desc' => $this->l('Choose between Yes and No.'),
                            'cast' => 'boolval',
                            'type' => 'bool',
                            'identifier' => 'id_PS_MYMODULE_OPTION1',
                        ),
                        'PS_MYMODULE_OPTION2' => array(
                            'title' => $this->l('Add some text'),
                            'desc' => $this->l('This is where you can add some text'),
                            'cast' => 'strval',
                            'type' => 'text',
                            'size' => '10',
                            'identifier' => 'id_PS_MYMODULE_OPTION2',
                        ),
			)
		)
		);
		$helper = new HelperOptions();
		$helper->module = $this;
		$helper->id=$this->id;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
		$helper->title = $this->displayName;
		$helper->show_toolbar = true;
		$helper->toolbar_scroll = true;
		$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')
			)
		);
		return $helper->generateOptions($fields_options);
	}

  • Like 1
Link to comment
Share on other sites

Where is your "form" ???

 

You should have

$fields_form = array(
			'form' => array( ....legend, description, etc...
//Then the input form with names!
'input' => array(

array(
'type'  => 'text',
'label' => $this->l('Add some text'),
'name'  => 'id_PS_MYMODULE_OPTION2',
'desc'  => $this->l('This is where you can add some text'),
'align' => 'text-center',
'class' => 'fixed-width-xs',  
'required' => true
),
....
See the doc or modules Edited by Eolia (see edit history)
Link to comment
Share on other sites

Looking in the class code HelperOptions.php,

the method $helper->generateOptions($fields_options) use the file admin6157\themes\default\template\helpers\options.tpl as template.
This template already contains a form, infact the final html code that I get is

<form action="index.php?controller=AdminModules&configure=mymodule&token=986f54fc864f06d2779e8674cc589bea"
	id="configuration_form"
			method="post"
	enctype="multipart/form-data">
					<fieldset >
[...]		
		<input type="radio" name="PS_MYMODULE_OPTION1" id="PS_MYMODULE_OPTION1_on" value="1" />
		<input type="radio" name="PS_MYMODULE_OPTION1" id="PS_MYMODULE_OPTION1_off" value="0"  checked="checked"/>
[...]

I don't think I need to use even a fields_form, do you?

Edited by nozzula (see edit history)
  • Like 1
Link to comment
Share on other sites

the name of your radio input is "PS_MY_MODULE_OPTION1", but you are using "id_PS_MYMODULE_OPTION1" in your Tools::getValue().  These need to match

<input type="radio" name="PS_MY_MODULE_OPTION1" id="PS_MY_MODULE_OPTION1_on" value="1" />
$id_PS_MYMODULE_OPTION1 = Tools::getValue('id_PS_MYMODULE_OPTION1');
Link to comment
Share on other sites

modified in
 

$PS_MYMODULE_OPTION1 = Tools::getValue('PS_MYMODULE_OPTION1');
$PS_MYMODULE_OPTION2 = Tools::getValue('PS_MYMODULE_OPTION2');
die('test=#' . $PS_MYMODULE_OPTION1 . '#' . $PS_MYMODULE_OPTION2 . '#');

still got empty value from getValue
"test=###"

:(

Link to comment
Share on other sites

I removed the second option (PS_MYMODULE_OPTION2), anyway still not work.

 

I noticed that:

the method generateOptions() create a form POST without the submit button...

And the save button inserted in the toolbar submit as GET request.

 

I guess there's something wrong in this and the problem is there...

Edited by nozzula (see edit history)
Link to comment
Share on other sites

using the browsers developers tools (f12 button), you need to confirm what information is being sent from the browser to the server.  this is not rocket science.  you create a form with input fields, when you submit the form, those input parameters and values are sent to the server.

 

start simple and just confirm that the information you are expecting to be sent, are in fact being sent.

Link to comment
Share on other sites

Adding an array 'submit' in fields_options (I don't know why but) everything works fine.

Here's the final code:

 

Thank you guys

	public function getContent()
	{
		if (Tools::isSubmit('submitOptionsconfiguration'))
		{
			$PS_MYMODULE_OPTION1 = Tools::getValue('PS_MYMODULE_OPTION1');
			Configuration::updateValue('PS_MYMODULE_OPTION1', $PS_MYMODULE_OPTION1);
		}
		
		return $this->displayForm();
	}

	public function displayForm()
	{
		$fields_options = array(
			'general' => array(
				'title' => $this->l('Parameters'),
				'image' => '../img/admin/information.png',
				'fields' => array(
					'PS_MYMODULE_OPTION1' => array(
						'title' => $this->l('Choose one'),
						'desc' => $this->l('Choose between Yes and No.'),
						'cast' => 'boolval',
						'type' => 'bool',
					),
				),
    			'submit' => array(
					'title' => $this->l('Save'),
					'class' => 'button'
				),
			)
		);
		$helper = new HelperOptions();
		$helper->module = $this;
		$helper->id=$this->id;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
		$helper->title = $this->displayName;
		$helper->show_toolbar = true;
		$helper->toolbar_scroll = true;
		$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')
			)
		);
		return $helper->generateOptions($fields_options);
	}
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...