Динамическое скрытие полей формы для различных действий

magento.stackexchange https://magento.stackexchange.com//questions/70424

Вопрос

у меня 2 действия IndexController: newAction() и editAction().

Оба этих действия будут использовать одну и ту же форму (около 5 входных данных: First Name, Last Name, Email, Status, Example)

Но в newAction(), я просто хочу показать 2 входа: Электронная почта и статус, пока в editAction(), Я хочу показать полные 5 входов.

Как я могу это сделать?

Большое спасибо

РЕДАКТИРОВАТЬ:

Привет @Михай МАТЕИ!

Я обновил свой код, это мой _prepareForm() функция

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();
    }

Но когда я перехожу к newAction, он все равно показывает все остальные поля.Что не так с моим кодом?Спасибо

Это было полезно?

Решение

Будет лучше использовать registry(Mage::registry()).

В реестре будет идентификатор всякий раз, когда an object is created and it data exits

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

Код:

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();
    }

Другие советы

Вы можете использовать getActionName() метод, чтобы проверить, какое действие было вызвано, и соответствующим образом отобразить ваши входные данные:

$actionName = Mage::app()->getRequest()->getActionName();
if($actionName == 'edit'){
//...add the other three input fields
}    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top