Monday, April 29, 2013

magento add custom validation to admin section field

well... i had to create my own custom validation class for my magento extension because the client said that.
'For Video title- it should accept alphabets[A-Za-z], numerics and also special charater "Space" , and also it should not start with number'.
since changing core file is a bad habit , so i created  my own js file and add my custom validation class.But first let me tell u how u can do it fast by modifying core file.
//BAD BUT FAST WAY...
open  js/prototype/validation.js

some where at line 479-771, u will see lots of validation classess that magento uses. U can define your own validation class like i did.

I added below code after line 489.

  ['validate-alphanum-with-spaces-char-first', 'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.', function(v) {
                return Validation.get('IsEmpty').test(v) ||  /^[a-z]+[a-z0-9 ]+$/.test(v)
            }],


 //NOW THE STANDARD WAY....
1. add your custom js file to the module.
     in my case it was video edit section in admin:

 app/design/adminhtml/default/default/layout/mymod_video.xml
<layout>
<adminhtml_video_video_edit>
     <reference name="head">
                    <action method="addJs"><script>alfasoft/videoext/myvalidation.js</script></action>
        </reference>

        <reference name="content">
          ....
        </reference>
        <reference name="left">
          ....
        </reference>
    </adminhtml_video_video_edit>

</layout>

2. upload your js i.e. myvalidation.js to the location js/alfasoft/videoext/

3. Add your code to myvalidation.js
   if(Validation)
    {
        Validation.addAllThese([
           
            ['validate-alphanum-with-spaces-char-first', 'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.', function(v) {
                return Validation.get('IsEmpty').test(v) ||  /^[a-z]+[a-z0-9 ]+$/.test(v)
            }]     // this is my validation requirement, your could be anything else.


        ]);
    }

Now Let me explain the code. according to my requirement.

>the regular expression fit the best /^[a-z]+[a-z0-9 ]+$/.test(v) . your could be anything else.
>here my validation class name is  'validate-alphanum-with-spaces-char-first'. 
>And my error message is:
'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.'

 Use your class in the form:
mine path was:
app/code/community/Alfasft/Video/Block/Adminhtml/Video/Edit/Tab/Form.php

$fieldset->addField('vtitle', 'text', array(
            'label' => Mage::helper('video')->__('Video Title'),
            'name'  => 'vtitle',
            'required'  => true,
            'class' => 'required-entry validate-alphanum-with-spaces-char-first',

        ));


 //I have assign this class to the field 'vtitle'.





Friday, April 26, 2013

magento validation alpha numeric with spaces

Magento has a class , use this for  this validation

validate-alphanum-with-spaces

Monday, April 8, 2013

magento admin redirect to same url after post

 Put the below code at the end of the action:

$this->_redirectReferer($this->getRequest()->getServer('HTTP_REFERER'));
        return;

Sunday, February 10, 2013

GET QUOTE ITEMS PRODUCT COUNT BY ATTRIBUTE


This code is for counting the total number to products in the cart having specific attribute code.

$items =Mage::getModel('checkout/cart')->getQuote()->getAllItems();
  $attriubtecnt=0;
 
   foreach($items as $item)
    {
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$att_value= $_product->getAttributeText('attributecode');

if(trim($att_value)=='lining')
{
                      $liningcnt +=$item->getTotalQty();
                 }

}

Wednesday, November 21, 2012

MAGENTO CHANGE CAPTCHA BACKGROUND LINES AND DOTS DENSITY

I had to reduce the density of dots and lines in captcha because it was un readable sometimes.

so here what i did.

Go to:

lib/Zend/Captcha/Image.php

protected $_dotNoiseLevel = 10;
protected $_lineNoiseLevel = 0;



magento:IP SPECIFIC LANGUAGE

 First download currency switcher from   
 http://www.magentocommerce.com/magento-connect/auto-currency-switcher-8671.html  
 //CODE TO DISPLAY LANGUAGE ACCORDING TO IP ADDRESS  
  if(Mage::getConfig()->getModuleConfig('Chapagain_AutoCurrency')->is('active', 'true'))  
  {   
                $geoIp = Mage::helper('autocurrency')->loadGeoIp();  
                $ipAddress = Mage::helper('autocurrency')->getIpAddress();  
                // get country code from ip address  
                 $countryCode = geoip_country_code_by_addr($geoIp, $ipAddress);   
                 $germancountries=array('CH','DE');  
                if(!isset($_COOKIE['frontstoreloc']))   
                {   
                 //$siteurl= $this->getUrl();  
                 $siteurl= "http://".$_SERVER['SERVER_NAME'];  
                      setcookie("frontstoreloc",session_id(),time()+60*60*24,"/","");  
                     switch(trim($countryCode))  
                     {  
                           case 'CH':  
                                    $url = $siteurl . '?___store=german';  
                                    header( 'Location:' . $url);die;  
                           break;  
                           case 'DE':  
                                         $url = $siteurl . '?___store=german';  
                                         header( 'Location:' . $url);die;  
                           break;  
                           case 'IN':  
                                     $url = $siteurl . '?___store=english';  
                                     header( 'Location:' . $url);die;  
                           break;  
                           default:  
                           $url = $siteurl . '?___store=usa';  
                                     header( 'Location:' . $url);die;  
                           break;  
                     }  
                 }  
            }  

MAGENTO GET STORE SPECIFIC ATTRIBUTE LABEL:

 <?php echo $_product->getResource()->getAttribute($_attribute->getAttributeId())->getStoreLabel();?>