Jump to content
Pedro Lima

Conditional fields based on select option inside renderForm

Recommended Posts

	$some_options = array(
            array(
                'id_option' => 'optone',
                'name' => $this->l('Option 1')
            ),
            array(
                'id_option' => 'opttwo',
                'name' => $this->l('Option 2')
            ),
        );

        $fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('This is the title'),
                    'icon' => 'icon-cogs'
                ),
                'description' => $this->l('Here the description...'),
                'input' => array(
                    array(
                        'type' => 'switch',
                        'is_bool' => true, //retro-compat
                        'label' => $this->l('Enable'),
                        'name' => 'BTN_ENABLE_0',
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => 1,
                                'label' => $this->l('Enabled')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => 0,
                                'label' => $this->l('Disabled')
                            )
                        ),
                    ),
                    array(
                        'type' => 'select',
                        'lang' => true,
                        'label' => $this->l('Select an Option'),
                        'name' => 'SELECT_OPT',
                        'desc' => $this->l('Just select the option...'),
                        'options' => array(
                            'query' => $some_options,
                            'id' => 'id_option',
                            'name' => 'name'
                        )
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Username'),
                        'name' => 'USERNAME',
                        'suffix' => 'USER',
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Password'),
                        'name' => 'PASSWORD',
                        'suffix' => 'SECRET',
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Some Code'),
                        'name' => 'SOME_CODE',
                        'suffix' => 'CODE',
                    ),
                    array(
                        'type' => 'desc',
                        'name' => '',
                        'text' => $this->l('And here another random text or whatever...')
                    ),
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                    'class' => 'btn btn-default pull-right'
                )
            ),
        );

So I have this code and I want to show "Username" and "Password" fields only when I have "Option 1" selected and hide "Some Code" field.

Then the oposite when "Option 2" is selected. Hide both "Username" and "Password" fields and show "Some Code" field.

And that's it, I've already googled, searched here inside forum and just can't find the answer, which I believe to be something simple.

Thank you very much in advance.

Share this post


Link to post
Share on other sites

Is Prestashop dying? I mean, I use other Open Source solutions for ecommerce and the communities are WAYYY more interactive!

Share this post


Link to post
Share on other sites

We kindly ask you to stop upping your question. If there is no answer, perhaps your question is not clear enough. What do you want to do ? Specify, for what it is. Module ?

Share this post


Link to post
Share on other sites

Hi,

Thank you for your answer.

Yes, it is for a new module I am personally testing but in order to make it work I need to have a selectbox.

I will try to explain in steps, maybe I can be more clear, so let's see:

  1. We get inside that Module configuration page;
  2. We have a selectbox with 2 different options (let's call it Option 1 and Option 2);
  3. I can select one of those two options (the common functionality of any selectbox);
  4. If I select "Option 1", bellow that selectbox it should appear two text fields (I named those fields "Username" and "Password" but those are just text fields);
    1. No other fields should be visible, only these 2;
  5. If I select "Option 2", bellow that selectbox it should appear one field (I named that field "Some Code" and it should be just a text field);
    1. No other fields should be visible, only this 1.

And that's it.

I tried Googling this, I saw that there are modules doing it so it's "duable" and I just wanted to see if the community could help.

If not, I understand that and this thread can be closed and I will not bother the community again with "my problems" (that can be other one problems too). But it's kinda sad because I usually use a lot other platforms and the communities are way more interactive. :(

Share this post


Link to post
Share on other sites

Hi Pedro 

You can try to add for example admin.js file and "hook" it into  back office header hook "hookBackOfficeHeader" .

There is js file you could add events that will watch when select is clicked and hide options you do not want to show.

You target select by name :

$(document).ready(function() {

    $(document).on('change', "input[name='SELECT_OPT']", function() {
        if ( contition ) {
        	$('input[name="USERNAME"]').hide()
            $('input[name="PASSWORD"]').hide();
        } else {
        	$('input[name="USERNAME"]').show()
            $('input[name="PASSWORD"]').show();
        }
    });

});

Just example code you probably need bit more code.

 

And it is true that community have maybe less active people but that just mean you have to wait a bit for answer.

You can bump in a week or check if your  original question needs more info, but topic should not be closed and you do not bother anyone.

Also if you do not receive answer on forum and no similar topics and it is connected with code and development you can ask also on https://gitter.im/PrestaShop/General

There usually after day or two developers from PrestaShop Team see question and may give quick response. 

  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More