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'.





3 comments:

  1. Thanks man, you saved my day!

    ReplyDelete
    Replies
    1. Welcome Matteo, I am a magento developer having 5 years of working experience, let me know if you need any kind of help in future.
      Thanks

      Delete
  2. I need to add these kind of validation to my product form i nadmin panel. Where can I find Form.php for it?

    ReplyDelete