Jump to content

How Set Selected option in a HelperForm


ZiedDams

Recommended Posts

I'm developing a configuration form using  the Helper-form , my form has a <select> tag with list of <options> , all this is working my problem is i want to set a specific <option> selected,  In another way i want to add an attribute selected to  an option in condition of the value , Any Help ?

Link to comment
Share on other sites

5 hours ago, knacky said:

this is my "$fields_form"

$fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->getTranslator()->trans(
                        'Slide information',
                        array(),
                        'Modules.TvcmsSlider.Admin'
                    ),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'file_lang',
                        'label' => $this->getTranslator()->trans('Image', array(), 'Admin.Global'),
                        'name' => 'image',
                        'required' => true,
                        'lang' => true,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->getTranslator()->trans('Title', array(), 'Admin.Global'),
                        'name' => 'title',
                        'lang' => true,
                    ),
                    array(
                        'type' => 'radio_btn',
                        'label' => $this->getTranslator()->trans('Text Alignment', array(), 'Admin.Global'),
                        'name' => 'class_name',
                        'lang' => true,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->getTranslator()->trans('Button Caption', array(), 'Admin.Global'),
                        'name' => 'btn_caption',
                        'lang' => true,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->getTranslator()->trans('Target URL', array(), 'Modules.TvcmsSlider.Admin'),
                        'name' => 'url',
                        'required' => true,
                        'lang' => true,
                    ),
                    // array(
                    //     'type' => 'hidden', // This is Not Use in This Theme
                    //     'label' => $this->getTranslator()->trans('Caption', array(), 'Modules.TvcmsSlider.Admin'),
                    //     'name' => 'legend',
                    //     'lang' => true,
                    // ),
                    array(
                        'type' => 'textarea',
                        'label' => $this->getTranslator()->trans('Description', array(), 'Admin.Global'),
                        'name' => 'description',
                        'autoload_rte' => true,
                        'lang' => true,
                    ),
                    array(
                        'type' => 'switch',
                        'label' => $this->getTranslator()->trans('Enabled', array(), 'Admin.Global'),
                        'name' => 'active_slide',
                        'is_bool' => true,
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => 1,
                                'label' => $this->getTranslator()->trans('Yes', array(), 'Admin.Global')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => 0,
                                'label' => $this->getTranslator()->trans('No', array(), 'Admin.Global')
                            )
                        ),
                    ),
                    array( // this is the select 
                        'type' => 'select',
                        'label' => $this->l('Select an Auction for the slide'),
                        'name' => 'hit_auction_slide_id',
                        'desc' => $this->l("If you choose an auction for the slide you the slide will be display all informations about the auction in the homepage"),
                        'options' => array(
                            'query' => $def_options_auctions,
                            'id' => 'id_auction',
                            'name' => 'name',
                        ),
                        'class' => 'fixed-width-x2',
                    )
                ),
                'submit' => array(
                    'title' => $this->getTranslator()->trans('Save', array(), 'Admin.Actions'),
                )
            ),
        );

and this is my options list variable

$list_auctions = $this->getAuctions();
$def_options_auctions = array();

       foreach ($list_auctions as $auction) {
            $prod = new Product((int)$auction['id_product'],true,$this->context->language->id);
            $def_options_auctions[] = array(
                'id_auction' => $auction['id_auction_premium_auction'],
                'name' =>$prod->name,
            );
        }

finally this my Helper Form

 $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
        $helper->default_form_language = $lang->id;

        $tmp = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG');
        $helper->allow_employee_form_lang = $tmp ? $tmp : 0;
        $this->fields_form = array();
        $helper->module = $this;
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitSlide';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.
            '&tab_module='.$this->tab.'&module_name='.$this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
        $helper->tpl_vars = array(
            'base_url' => $this->context->shop->getBaseURL(),
            'language' => array(
                'id_lang' => $language->id,
                'iso_code' => $language->iso_code
            ),
            'fields_value' => $this->getAddFieldsValues(),
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
            'image_baseurl' => $this->_path.'views/img/'
        );
        $helper->fields_value['hit_auction_slide_id'] = "6"; // saddly  not working yet 
        $helper->override_folder = '/';

        $languages = Language::getLanguages(false);

        if (count($languages) > 1) {
            return $this->getMultiLanguageInfoMsg() . $helper->generateForm(array($fields_form));
        } else {
            return $helper->generateForm(array($fields_form));
        }

 

 

Link to comment
Share on other sites

Hi.

You are using a function call to find values.

'fields_value' => $this->getAddFieldsValues(),

 

So enter your value in the function for reading values.
E.g:

public function getAddFieldsValues()
{
	$values = array();
	$values['hit_auction_slide_id'] = '6';

  	....

	return $values;
 }

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

3 minutes ago, knacky said:

Hi.

You are using a function call to find values.

'fields_value' => $this->getAddFieldsValues(),

 

So enter your value in the function for reading values.
E.g:

public function getAddFieldsValues()
{
	$values = array();
	$values['hit_auction_slide_id'] = '6';

  	....

	return $values;
 }

 

thank you so much it works now

  • Like 1
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...