For validation of any field in admin & front end, all fileds are having css class.
1.<input type="text" class="required-entry validate-namemaxlength input-text" title="First Name" value="" name="firstname" id="firstname"/>
2.<select class="validate-select" title="Gender" name="gender" id="gender">
<option value="">Select</option>
<option value="123">Male</option>
<option value="124">Female</option>
</select>
3.<input type="text" class="required-entry validate-zip validate-zipmaxlength input-text" id="zip" title="Zip/Postal Code" value="" name="postcode"/>
4.<input type="password" class="required-entry validate-password input-text" title="Password" id="password" name="password"/>
5.<input type="password" class="required-entry validate-cpassword input-text" id="confirmation" title="Confirm Password" name="confirmation"/>
6.<textarea cols="15" rows="2" class="required-entry validate-minlength validate-maxlength required-entry textarea" style="width: 450px; height: 500px;" title="Content" name="content" id="content">Best of tees on Tshirt Crack</textarea>
You can see the number of examples of different type of fields.
Basic Idea is , you need to make validation class in validation.js file for any new validation type.
After that need to add that class to particular field. If multiple validation is there, i.e. Field is required, Field should have minimum length of 10 character, Field should have maximum length of 100 character, Field must follow some regex(regular expression) pattern.
Then priority can be set like, First need to check required, second minimum length, third maximum length, forth regex pattern matching.
Steps for creating new validation class ,
Step1:
Go to file, js/prototype/validation.js in main magento folder.
Step2:
In Validation.addAllThese() , all validation classes are defined.
Validation.addAllThese([
['validate-select', 'Please select an option.', function(v) {
return ((v != "none") && (v != null) && (v.length != 0));
}],
['required-entry', 'This is a required field.', function(v) {
return !Validation.get('IsEmpty').test(v);
}],
|
|
|
|
[.....................................................
..],
[.....................................................
..] ] )
Step3:
If you want to add new validation, you need to add only,
['validate-titlemaxlength', 'Maximum 100 characters are allowed.', function (v, elm) {
if(v.length > 100)
{
return false;
}
else
{
return true;
}
}],
Here validate-titlemaxlength is your class which needs to be placed in css class of particular field. 'Maximum 100 characters are allowed.' is error message.
In function (v, elm) , v is the value of field and elm is object of element by which we can access any property of field.
Steps to place new class Or modify existing allocation of validation classes at frontend,
Step1 :
For any form field in front end go to the template file of that particular module.
i.e. app/design/frontend/default/default/template/customer/form/register.phtml
Step2 :
ex, <div class="input-box">
<label for="zip"><?php echo $this->__('Zip/Postal Code') ?> <span class="required">*</span></label><br/>
<input type="text" name="postcode" value="<?php echo $this->htmlEscape($this- >getFormData()->getPostcode()) ?>" title="<?php echo $this->__('Zip/Postal Code') ? >" id="zip" class="required-entry validate-zip validate-zipmaxlength input-text" />
</div>
In above code in class of zip code , required-entry validate-zip validate-zipmaxlength is there.
Now, if you want to add minimum length validation i.e. validate-zipminlength, then create new class in validation.js as mentioned before and then just append to existing classes by space in field according to priority , i.e. required-entry validate-zip validate-zipminlength validate-zipmaxlength .
Steps to place new class Or modify existing allocation of validation classes at backend,
Step1:
Go to app/code/core/Mage/Adminhtml/Block/ModuleName/Edit/Tab/Form.php,
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('contest')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
Step2: ));
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('contest')->__('Title'),
'class' => 'required-entry validate-title',
'required' => true,
'name' => 'title',
));
Hi
ReplyDeleteThat Magento eCommercevalidation field setup is gave step by step is very nice.
sukumar
Magento Developers.
I want to modify some HTML Templates. Can anyone help me in that? And will I loose the old modification?
ReplyDeleteMagento Web Templates