1

I have custom product attribute called Donation, I created it by

Catelog->Attribute->Manage-Attribute   Add New Attribute.

Their is no problem, Its working fine, But I need to add validation for this fields,

while creating product admin should give number from 20 to 100, I am mentioning percentage here and 20 is the minimum donation percentage,

I am not able to do this, help me, How to validate this? or how can I restrict the admin to choose number between 20 to 100,? Thanks in Advance:)

  • you can add custom validation in validation.js file in js/prototype.and add class to your input – sivakumar Jul 24 '14 at 11:07
  • you can follow this link. magento.stackexchange.com/questions/15163/… – sivakumar Jul 24 '14 at 11:08
  • @sivakumar, I created this custom attribute by Manage-Attribute, How Can I Add class to select in backend – Charlie Jul 24 '14 at 11:12
1

One option that would not involve any custom code would be to make this option a select field and then add all the option values from 20-100. Sure it may not be the prettiest option but would work nicely.

If you wanted to not have to add all the options to the attribute you could create this attribute and set a source model. In this source model you can then populate the select and not have to add all the options yourself.

Simply setup your attribute in a setup script with the source model

$setup->addAttribute('catalog_product', 'custom_select', array(
    'input' => 'select',
    'type' => 'int',
    'label' => 'Custom Select',
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'source' => 'mymodule/selectoptions',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

And then add your source model code.

class Mypackage_Mymodule_Model_Selectoptions extends Mage_Eav_Model_Entity_Attribute_Source_Abstract {

    public function getAllOptions($withEmpty = true)
    {
        // This code could be done in a for loop :)
        $options = array(
            array('label' => '20', 'value' => 20),
            array('label' => '21', 'value' => 21),
            array('label' => '22', 'value' => 22),
            array('label' => '23', 'value' => 23),
            ....
        );
        if ($withEmpty) {
            array_unshift($options, array('label' => '', 'value' => ''));
        }

        return $options;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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