Magento – How to repopulate form data in magento if email already available

formsmagento-1.8session

I created cumstom module for seller registration in magento.
On email validation if email is already available…my form reloading with error email already available…

On that time my form whole value goes empty….

I need to know that how to repopulate data in fields on …error in magento..on form reloading…

my one line html is..


  <li>
                    <label for="name" class="required"><em>*</em><?php echo $this->__('Enter Your Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="vname" id="vname" value="" title="<?php echo $this->__('Enter Your Name') ?>" class="input-text  required-entry" />
                    </div>
                </li>

                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="text" name="email" id="email_address" value="" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
                    </div>
                </li>

Best Answer

One way to accomplish this is to save the info to your session, take a look at Magento - Wiki - Custom Module with Custom Database Table

Note : Although this example is base off admin form it can be adjusted for any situation.

On error save to session

class <Namespace>_<Module>_Adminhtml_<Module>Controller extends Mage_Adminhtml_Controller_Action
{
    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $<module>Model = Mage::getModel('<module>/<module>');
               ....
                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->set<Module>Data(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->set<Module>Data($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }

Display info in form

class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
       ....
        if ( Mage::getSingleton('adminhtml/session')->get<Module>Data() )
        {
            $form->setValues(Mage::getSingleton('adminhtml/session')->get<Module>Data());
            Mage::getSingleton('adminhtml/session')->set<Module>Data(null);
        } elseif ( Mage::registry('<module>_data') ) {
            $form->setValues(Mage::registry('<module>_data')->getData());
        }
Related Topic