Custom Validations for Product Options In Opencart Frontend

In this tutorial, we are going to cover the changes required to add the validation in the front end side. Before moving ahead, make sure that you read the previous article and do the changes required for the admin section. This tutorial is the second part of my previous post Product Option Validation in Opencart .
Here we are going to add two validation rules for the text product options:
Rule 1 : Restrict the numbers of characters
Rule 2 : Match the entered value with your pattern (regex)
Step : 1
Open file /catalog/controller/checkout/cart.php
and search for the below code for the product options foreach loop: (approx line no:309)
foreach ($product_options as $product_option) {
now add the below code just after the first if condition(for the required field) inside the above loop,
/*custom validation code start*/
else if ($product_option['validationrule']=='13' && $option[$product_option['product_option_id']]!='') {
$check = $this->validateForm($option[$product_option['product_option_id']],13);
if ($check=='13') {
$json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_13max'), $product_option['name']);
} else if ($check=='0') {
$json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_special'), $product_option['name']);
}
}
/*custom validation code end*/
Here, note that we have used one custom validation function called validateForm()
. So you need to add the below function at last in this file inside the main class. The regex is just for the demo test, you can add any rule as per your requirement(if required). The second inside condition is for checking the length of the input characters.
/*added new function for the regex validation*/
protected function validateForm($field,$maxlength) {
if ($field!='') {
if(preg_match("/^[A-Za-z0-9 \.\~\,\/\-\']+$/", $field)){
if ((utf8_strlen($field) > $maxlength)) {
return $maxlength;
}
} else {
return 0;
}
}
}
Step : 2
Open /catalog/model/catalog/product.php
file and search for the below code inside the $product_option_data[]
array (approx line no : 348)
'required' => $product_option['required']
and add the below line just after that.
'validationrule' => $product_option['validationrule']
make sure to add ',' after required option :
'required' => $product_option['required'],
'validationrule' => $product_option['validationrule']
Step : 3
Open /catalog/language/english/checkout/cart.php
file and search for the below line:
$_['error_required'] = '%s required!';
add the below lines of code just after that:
$_['error_special'] = 'Please enter only allowed characters for %s!';
$_['error_13max'] = 'Max 13 characters are allowed!';
That's it..!! You're done with the Front end section. Now create a new product(or modify existing) and add the custom validation rule from the admin section and then go to that product in front end side. Click add to cart with following cases:
- Without filling the value
- Adding not allowed characters (as per your regex set in the function)
- Adding characters more than 13
Check everything is working as expected or not. If you face any error or having any trouble with the implementation than make sure that you followed both of the articles for the product options validation correctly and even though you face any issue then feel free to write a mail to me, I will help you to sort it out your issues.
Scope of enhancement and modifications
The concept presented here is very basic and just to show how you can add your extra validation rule in opencart with the only default required option. In real application or scenario you might need to modify this code to fulfill your requirements. Below are some of cases from my side that can be included in this concept.
- You can multiple validation rules
- You can fire validation rule for specific categories only
- You can add validation rules for all the possible options (like radio, checkbox etc.)
- You can add multiple and more complex regex in the validation
- You can have conditional hierarchy of the rules also.
All these points are depended upon the requirements you have. I just provided the way how it can be accomplished. Now its upto you, how you utilize it.