0

I have added 2 custom attribute on product form. 1.Package type 2.Quantity

MY package type can be single /multiple. When it is multiple , I want to show Quantity inputbox & also , want to apply required validations to it.. For Single ,it should be hidden.

How can I ado this ? & where can I add validations ?

I am new to magento ,so can anyone help me with this?

0

I am not sure but you can normally create 4 attributes from admin panel. And through JS you can hide those 2 attributes and make them required when it is necessary.

If this works, then this scenario is possible otherwise it is not possible to make them required field on multiple selection. Because then the scenario will be: You want to add that attribute only when multiple selection happens and its not possible to add attribute only at that time.

2
  • how can I do this through js ? where can I add js code? Dec 6 '16 at 4:29
  • You can include your JS in validation.js, because it is available through the admin section. And you have to check the attribute field id, it is named by attribute name. Now you can simply hide those attributes. And you can check the multiselect by Onselect method in JS. Dec 6 '16 at 8:41
0

I solved this by adding this in config.xml file :

<adminhtml>
  <layout>
    <updates>
      <adminhtml_catalog_product_edit>
        <file>custom_local.xml</file>
      </adminhtml_catalog_product_edit>
    </updates>
  </layout>
</adminhtml>

& placed custom_local.xml under \app\design\adminhtml\default\default\layout\ folder .

In custom_local.xml, I added my custom js file for both add & edit product as :

 <layout>
<adminhtml_catalog_product_new>
    <reference name="head">
                <action method="addJs"><script>admin-custom.js</script></action>
    </reference>
</adminhtml_catalog_product_new>

<adminhtml_catalog_product_edit>
    <reference name="head">
                <action method="addJs"><script>admin-custom.js</script>    </action>
    </reference>
</adminhtml_catalog_product_edit>
</layout>

I placed my admin-custom.js under js folder in root directory & added following validations :

if(Validation) 
{  
Validation.add('conditional-required', 'This is a required field.', function(v) {
    var pack_type = $('pack_type').getValue();
    if(pack_type != 0)
    {
        return ( (v != null) && (v.length != 0));
    }
    else
    {
        return true;
    }

});   
}
 document.observe("dom:loaded", bodyOnload);
 function bodyOnload() {

$('bottle_litres').addClassName('required-entry');
//alert($('pack_type').getValue());
var pack_type = $('pack_type').getValue();
if(pack_type == 0) {
    $('no_of_bottles').closest('tr').hide();
    $('no_of_bottles').removeClassName('conditional-required');
} else {
    $('no_of_bottles').closest('tr').show();
    $('no_of_bottles').addClassName('conditional-required');
}


$('pack_type').observe('change', function(e) {
    pack_type = $('pack_type').getValue();//alert(pack_type);
    if(pack_type == 0) {
        $('no_of_bottles').closest('tr').hide();
        $('no_of_bottles').removeClassName('conditional-required');
    } else {
        $('no_of_bottles').closest('tr').show();
        $('no_of_bottles').addClassName('conditional-required');
    }
 });

 }

& it worked :-)

2
  • I followed the mentioned way so that the class(conditional-required) adding to the field, but the message is not showing. how can i add the custom validation method in admin end.
    – senthil
    Oct 11 '17 at 12:11
  • Anyone guide me how can i integrate Custom Feedback Form Code Into Magento - magento.stackexchange.com/q/244255/57334
    – zus
    Sep 28 '18 at 10:08

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.