cancel
Showing results for 
Search instead for 
Did you mean: 

Customised login form, new field not populated on submit

SOLVED

Customised login form, new field not populated on submit

Hi,

I'm writing a customised login form which incorporates both the default login email & password, but also a "pre registration" form which asks the user for an email address.  This then gets validated, and if it passes (e.g. doesn't already exist, can receive email, etc) they get dispatched to the proper registration page.

 

The problem I'm having is that if validation fails, the user is returned to the login page with an appropriate error message, but the email address input text box is not populated with the email address they previously entered.

 

At the moment my login page source looks like this:

<label for="regemail" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
<div class="input-box">
	<input type="text" name="regemail" value="<?php echo $this->escapeHtml($this->getRegemail()) ?>" id="regemail" class="input-text required-entry validate-email" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" />
</div>

I haven't created a "getRegemail" property/function, none of the guides I've seen about adding custom fields to customer registration define "getMyCustomField", so I presume something makes this work?

 

getUsername() works on the original login form, even though the form field is called "login[username]", so I guess I'm missing a piece of the puzzle.  For example - when you try and log in with the wrong password, you get returned to the login page but with the email address you entered already populated.

 

The problem I've got is that when the form is submitted - and validation fails - the user is returned to the login page by my controller indexAction, but the "regemail" field is not populated - i.e.

$this->escapeHtml($this->getRegemail())

..is blank.

 

What am I doing wrong here?  It feels like it should be something obvious but I'm not getting it Smiley Sad

 

Thanks in advance.

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

In the original form the fields values are stored in the current session (I don't remember exactly but in adminhtml there is something like formData. Is a session with the form values so that's how you can populate the form again after a fail.

I guess you should explore that part to be able to maintain your custom value on session (as the others values).

 

View solution in original post

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

I mean you shoul dbe able to use ->getYourfield()

In the same way you're using ->getUsername()

View solution in original post

9 REPLIES 9

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

Has you created the attribute for customers or is just the html field?

Re: Customised login form, new field not populated on submit

Hi

I haven't created any customer fields. All I'm trying to do is add a "Confirm Email" field on the registration form, and customise the login form so that it asks for an email address in advance of the user clicking on the "Register" button, if that makes sense. The "Confirm Email" field won't be saved anywhere, it's just used for validation.

I've got the "pre registration form" posting to my custom controller, doing the validation, and redirecting back to the login form if there was a problem - but I can't get the email field on the pre-registration form on the login page to populate.

I hope that makes sense? On the original Magento login form if you enter your email and the wrong password, you get redirected back to the same page (login.phtml) with the email you entered before submitting the form populated. That's what I'm trying to achieve for my custom forms.

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

In the original form the fields values are stored in the current session (I don't remember exactly but in adminhtml there is something like formData. Is a session with the form values so that's how you can populate the form again after a fail.

I guess you should explore that part to be able to maintain your custom value on session (as the others values).

 

Re: Customised login form, new field not populated on submit

Thanks.

In the original login.phtml form, the input field is populated with getUsername(). Do I need to create my own function like this?

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

If you get the value into the session first you should be able to use the magic getter.

Re: Customised login form, new field not populated on submit

Sorry, "magic getter" ? Smiley Happy This is the first time I've tried to do this so am not well versed in it Smiley Sad

Thanks again!

Re: Customised login form, new field not populated on submit

Hi @DazOG,

 

I mean you shoul dbe able to use ->getYourfield()

In the same way you're using ->getUsername()

Re: Customised login form, new field not populated on submit

Excellent, thanks. So I don't have to actually create a getMyField() function if I populate the session form data properly?

Could you point me in the direction of any documentation about this stuff (eg. magic getter), so I don't have to bother you with questions? Thanks.

Re: Customised login form, new field not populated on submit

Managed to solve this, thanks.

 

As you pointed out - on the core login controller (AccountController.php) the email the user enters is saved in the session via:

$session->setUsername($login['username']);

..and then recovered on login.phtml via Mage/Customer/Block/Form/Login.php using

$this->getUsername

 

I approximated this without extending the Block code by using:

<input type="text" name="reg-email" value="<?php echo $this->escapeHtml(Mage::getSingleton('customer/session')->getPreRegEmail(true)) ?>" id="reg-email" class="input-text required-entry validate-email" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" />

..after having saved the email the user entered in my custom controller with:

Mage::getSingleton('customer/session')->setPreRegEmail(<EMAIL>);

Thanks for the pointers in the right direction! Smiley Happy