2

I have 2 action in IndexController: newAction() and editAction().

Both those actions will use a same form (about 5 inputs: First Name, Last Name, Email, Status, Example)

But in the newAction(), I just want to show 2 inputs: Email and Status, while in the editAction(), I want to show full 5 inputs.

How can I do this?

Thank you very much

EDIT:

Hi @Mihai MATEI,

I've updated my code, this is my _prepareForm() function

protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        if (Mage::getSingleton('adminhtml/session')->getSimpleAffiliateData()) {
            $data = Mage::getSingleton('adminhtml/session')->getSimpleAffiliateData();
            Mage::getSingleton('adminhtml/session')->setSimpleAffiliateData(null);
        } elseif (Mage::registry('simpleaffiliate_data'))
            $data = Mage::registry('simpleaffiliate_data')->getData();

        $fieldset = $form->addFieldset('simpleaffiliate_form',
            array('legend' => Mage::helper('simpleaffiliate')->__('Account information')));
        $actionName = Mage::app()->getRequest()->getActionName();
        if ($actionName == 'new') {
            $fieldset->addField('customer_id', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Customer'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'customer_id',
                'values' => $this->getListCustomer(),
                'tabindex' => 1
            ));
            $fieldset->addField('status', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Status'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'status',
                'values' => array('1' => 'Enable', '2' => 'Disable'),
                'tabindex' => 1
            ));
        } else if ($actionName == 'edit') {
            $fieldset->addField('customer_id', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Customer'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'customer_id',
                'values' => $this->getListCustomer(),
                'tabindex' => 1
            ));
            $fieldset->addField('first_name', 'text',
                array(
                    'label' => 'First Name',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'first_name',
                ));
            $fieldset->addField('last_name', 'text',
                array(
                    'label' => 'Last Name',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'last_name',
                ));
            $fieldset->addField('email', 'text',
                array(
                    'label' => 'Email',
                    'class' => 'required-entry validate-email',
                    'required' => true,
                    'name' => 'email',
                ));
            $fieldset->addField('balance', 'text',
                array(
                    'label' => 'Balance',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'balance',
                    'readonly' => true,
                    'disabled' => true
                ));
            $fieldset->addField('total_received', 'text',
                array(
                    'label' => Mage::helper('simpleaffiliate')->__('Total Received'),
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'total_received',
                    'readonly' => true,
                    'disabled' => true
                ));
            $fieldset->addField('joined', 'date',
                array(
                    'label' => 'Joined',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'joined',
                    'image' => $this->getSkinUrl('images/grid-cal.gif'),
                    'input_format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                    'format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                    'time' => true
                ));
            $fieldset->addField('status', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Status'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'status',
                'values' => array('1' => 'Enable', '2' => 'Disable'),
                'tabindex' => 1
            ));
        }


        $form->setValues($data);
        return parent::_prepareForm();
    }

But when I go to newAction, it still shows every other fields. What wrong with my code? Thank you

2 Answers 2

0

It will be better idea to use registry(Mage::registry()).

The registry will have id whenver an object is created and it data exits

.
if(Mage::registry('simpleaffiliate_data'))->getId()):
// show First Name, Last Name,Example on edit action
endif;

Code:

protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        if (Mage::getSingleton('adminhtml/session')->getSimpleAffiliateData()) {
            $data = Mage::getSingleton('adminhtml/session')->getSimpleAffiliateData();
            Mage::getSingleton('adminhtml/session')->setSimpleAffiliateData(null);
        } elseif (Mage::registry('simpleaffiliate_data'))
            $data = Mage::registry('simpleaffiliate_data')->getData();

        $fieldset = $form->addFieldset('simpleaffiliate_form',
            array('legend' => Mage::helper('simpleaffiliate')->__('Account information')));
        $actionName = Mage::app()->getRequest()->getActionName();

            $fieldset->addField('customer_id', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Customer'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'customer_id',
                'values' => $this->getListCustomer(),
                'tabindex' => 1
            ));
            $fieldset->addField('status', 'select', array(
                'label' => Mage::helper('simpleaffiliate')->__('Status'),
                'class' => 'required-entry',
                'required' => true,
                'name' => 'status',
                'values' => array('1' => 'Enable', '2' => 'Disable'),
                'tabindex' => 1
            ));
        if (Mage::registry('simpleaffiliate_data'))->getId()) {

            $fieldset->addField('first_name', 'text',
                array(
                    'label' => 'First Name',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'first_name',
                ));
            $fieldset->addField('last_name', 'text',
                array(
                    'label' => 'Last Name',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'last_name',
                ));
            $fieldset->addField('email', 'text',
                array(
                    'label' => 'Email',
                    'class' => 'required-entry validate-email',
                    'required' => true,
                    'name' => 'email',
                ));
            $fieldset->addField('balance', 'text',
                array(
                    'label' => 'Balance',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'balance',
                    'readonly' => true,
                    'disabled' => true
                ));
            $fieldset->addField('total_received', 'text',
                array(
                    'label' => Mage::helper('simpleaffiliate')->__('Total Received'),
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'total_received',
                    'readonly' => true,
                    'disabled' => true
                ));
            $fieldset->addField('joined', 'date',
                array(
                    'label' => 'Joined',
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'joined',
                    'image' => $this->getSkinUrl('images/grid-cal.gif'),
                    'input_format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                    'format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                    'time' => true
                ));

        }


        $form->setValues($data);
        return parent::_prepareForm();
    }
0

You can use getActionName() method to check which action was called, and display your inputs accordingly:

$actionName = Mage::app()->getRequest()->getActionName();
if($actionName == 'edit'){
//...add the other three input fields
}    
2
  • Hi @Mihai MATEI I've updated my code. Please check. Thank you
    – 0xh8h
    Jun 9, 2015 at 2:51
  • Nothing wrong with the code you posted. I've checked the solution and works for me. Make sure you changed the correct file! Also check if there is some caching going on (some plugin caching forms, also clear Magento cache just to be sure) Jun 9, 2015 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.