Jump to content

[1.6.1.16] Remember input on custom field


Recommended Posts

I've added a custom field on the contact page following @NemoPS's tutorial & using overrides. I made some of my own additions, for example if the field is left empty, it'll generate an error when the form is submitted. I can't, however, accomplish one thing. Which is, I can't make the custom field remember its input whenever an error is generated & the page is refreshed.

 

How does all the other fields keep the same input as it had before the page gets refreshed? I checked .js files and the core files related to the contact page but I can't find anything.

 

In the contact-form.tpl file, each field has an {else} statement after the {if isset $field} tag, the input after the else statement has always a value like {$field|escape:'html':'UTF-8'}. In NemoPS's tutorial, he left the value empty. Example for the E-mail field:

                    <p class="form-group">
                        <label for="email">{l s='Email address'}</label>
                        {if isset($customerThread.email)}
                            <input type="text" id="email" name="from" value="{$customerThread.email|escape:'html':'UTF-8'}" readonly="readonly" />
                        {else}
                            <input type="text" id="email" name="from" data-validate="isEmail" value="{$email|escape:'html':'UTF-8'}" />
                        {/if}
                    </p>

So I thought, this was what's saving the inputs and I also put something similar to my input after the {else} statement, example:

 

                        {if isset($customerThread.myField)}
                            <input type="text" id="myField" name="myField" value="{$customerThread.myField|escape:'html':'UTF-8'}" maxlength="140" readonly="readonly" />
                        {else}
                            <input type="text" id="myField" name="myField" maxlength="140" value="{$myField|escape:'html':'UTF-8'}" />
                        {/if}

This only generated a Smarty error within the input field. I also tried changing $myField to $customerThread.myField but the same thing happened. Here's the error:

<br /><b>Notice</b>:  Undefined index: myField in <b>..\cache\smarty\compile\35\ec\c4\35ecc44ae0ee3f768d5bec38cc50486fa6cce03b.file.contact-form.tpl.php</b> on line <b>111</b><br /><br /><b>Notice</b>:  Trying to get property of non-object in <b>..\cache\smarty\compile\35\ec\c4\35ecc44ae0ee3f768d5bec38cc50486fa6cce03b.file.contact-form.tpl.php</b> on line <b>111</b><br />

Does anyone have any ideas on where the file responsible for saving fields is?

 

Here's an example: http://i.imgur.com/hzzULUz.jpg 

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

I am not sure I understand what you mean, are you assigning that variable in the controller, always?

Did you see the screenshot? Basically the field is not saving its input when the page reloads after getting an error (example if the user types invalid email and they get an error and the page reloads). All other fields are saved when that happens except for our custom one.

Link to comment
Share on other sites

like $this->context->smarty->assign('myField','value');

 

I think I actually found the solution thanks to your observation. The function initContent() in ContactController.php was the one responsible for remembering fields. I noticed the following lines in the function:

 

        $this->context->smarty->assign(array(
            'contacts' => Contact::getContacts($this->context->language->id),
            'message' => html_entity_decode(Tools::getValue('message'))
        ));

And I added a smarty context for our custom field: 

 

        $this->context->smarty->assign(array(
            'contacts' => Contact::getContacts($this->context->language->id),
            'message' => html_entity_decode(Tools::getValue('message')),
            'myField' => html_entity_decode(Tools::getValue('myField')
        ));

I'm not sure if "html_entity_decode" is necessary for the field but after adding this line, it now seems to remember the input of the custom field as well. 

 

This is how the field looks in contact-form.tpl:

 

{if isset($customerThread.myField)}
    <input type="text" id="myField" name="myField" value="{$customerThread.myField|escape:'html':'UTF-8'}" maxlength="140" readonly="readonly" />
 {else}
    <input type="text" id="myField" name="myField" maxlength="140" value="{$myField|escape:'html':'UTF-8'}" />
{/if}

Is what I did valid & correct? Sometimes when I do things & they work, something else gets messed up so I just want your confirmation. I'm wondering because there is also this within initContent():

 

        $email = Tools::safeOutput(Tools::getValue('from',
        ((isset($this->context->cookie) && isset($this->context->cookie->email) && Validate::isEmail($this->context->cookie->email)) ? $this->context->cookie->email : '')));
        $this->context->smarty->assign(array(
            'errors' => $this->errors,
            'email' => $email,
            'fileupload' => Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD'),
            'max_upload_size' => (int)Tools::getMaxUploadSize()
        ));

Should our custom field be added in that part instead? Should it be in both or is what I did sufficient? Either way, what I did seems to be working but I just want to be extra sure.

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

That should be okay :)

 

Thanks Nemo, you're the best! Speaking of things I did correctly, could you take a look at this topic as well:

 

https://www.prestashop.com/forums/topic/626097-moving-autocomplete-search-results-div-ac-results-into-search-block-wrapper/

 

Basically, it's about if I correctly overrode core JS/jQuery files. The solution and everything is presented in the topic, I just want your confirmation on that one as well. :)

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