0

In the administration area of Magento I am trying to create a dependant field. A dependant field is one that only becomes available/enabled depending on the value of, let's say, a drop down with 'Yes' or 'No' values. This is a built in feature of Magento as you can see from this blog post.

However the above blog post (and others I have found) assumes the fields are getting added in system.xml or using the method Vikram outlined below but I would like to add my dependency in my modules install script when I define my attributes, for example:

$installer->addAttribute(
    'catalog_category', 
    'show_dependant', 
    array(
        'label' => 'Show dependant?',
        'group' => 'My Group',
        'type' => 'int',
        'input' => 'select',
        'source'  => 'eav/entity_attribute_source_boolean',
        'required' => false,
        'visible' => true,
        'default' => '0',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    )
);


$installer->addAttribute(
    'catalog_category',
    'my_attribute_name',
    array(
        'label' => 'A New Attribute',
        'group' => 'My Group',   //will be created if necessary
        'type'  => 'int',
        'class' => 'validate-number',
        'required' => false,

        // Would be something like this maybe?
        'depends' => array('show_dependant', 1)

    )
);

Anyone know if this is even possible?

2 Answers 2

1

If you are working in MAGENTO ADMIN FORMS, consider this example for showing a text field only when the 'Specified' option is chosen. This method used admin form and not system.xml method.

$form = new Varien_Data_Form();

$form->addField('yesno', 'select', array(
    'label'  => $this->__('Yes or No?'),
    'values' => Mage::model('adminhtml/system_config_source_yesnocustom')
        ->toOptionArray(),
));
$form->addField('custom_value', text, array(
    'label'  => $this->__('Other'),
));

// Append dependency javascript
$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('yesno', 'yesno')
        ->addFieldMap('custom_value', 'custom_value')
        ->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'
);

You can add as many field mappings and field dependencies in this way as you wish.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for your help. I understand that I can add the dependencies in that way however my attributes get added via the install script that I created following asking this question: magento.stackexchange.com/questions/13255/…
Does Magento supports Field Dependence when using install scripts ?
Exactly my question :)
Mage uses Varien_Db_Ddl_Table class where you can configure all the fields and keys. Check all the methods and do not hesitate to look into the class definition which you are going to use, you can find a lot of interesting things for yourself :)
0

I have created simple category attribute dependency by adding new input renderer for attribute. It is working this way: You have several attributes:

– my_attribute
– my_attribute_text
– my_attribute_select

Note that they all start from my_attribute.

First attribute has boolean type. When it is set to true – other attributes that start from my_attribute is visible.

Source - https://github.com/elpas0/category_dependence

Description - http://nwdthemes.com/2015/02/20/magento-category-attributes-dependency/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.