<!-- Added these fields inside the registration form to make the fields -->
Code: Select all
<div class="row mb-3 required">
<label for="input-address" class="col-sm-2 col-form-label">{{ entry_address }}</label>
<div class="col-sm-10">
<input type="text" name="address" value="{{ address }}" placeholder="{{ entry_address }}" id="input-address" class="form-control" />
{% if error_address %}
<div class="text-danger">{{ error_address }}</div>
{% endif %}
</div>
</div>
<div class="row mb-3 required">
<label for="input-phone" class="col-sm-2 col-form-label">{{ entry_phone }}</label>
<div class="col-sm-10">
<input type="text" name="phone" value="{{ phone }}" placeholder="{{ entry_phone }}" id="input-phone" class="form-control" />
{% if error_phone %}
<div class="text-danger">{{ error_phone }}</div>
{% endif %}
</div>
</div>
<div class="row mb-3 required">
<label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
<div class="col-sm-10">
<input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control" />
{% if error_dob %}
<div class="text-danger">{{ error_dob }}</div>
{% endif %}
</div>
</div>
Code: Select all
<?php
namespace Opencart\Catalog\Controller\Account;
/**
* Class Register
*
* @package Opencart\Catalog\Controller\Account
*/
class Register extends \Opencart\System\Engine\Controller {
/**
* @return void
*/
public function index(): void {
if ($this->customer->isLogged()) {
$this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']));
}
$this->load->language('account/register');
$this->document->setTitle($this->language->get('heading_title'));
$data['breadcrumbs'] = [];
$data['breadcrumbs'][] = [
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
];
$data['breadcrumbs'][] = [
'text' => $this->language->get('text_account'),
'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language'))
];
$data['breadcrumbs'][] = [
'text' => $this->language->get('text_register'),
'href' => $this->url->link('account/register', 'language=' . $this->config->get('config_language'))
];
$data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')));
$data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
$data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
$data['config_telephone_display'] = $this->config->get('config_telephone_display');
$data['config_telephone_required'] = $this->config->get('config_telephone_required');
$this->session->data['register_token'] = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);
$data['register'] = $this->url->link('account/register.register', 'language=' . $this->config->get('config_language') . '®ister_token=' . $this->session->data['register_token']);
$data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language'));
$data['customer_groups'] = [];
if (is_array($this->config->get('config_customer_group_display'))) {
$this->load->model('account/customer_group');
$customer_groups = $this->model_account_customer_group->getCustomerGroups();
foreach ($customer_groups as $customer_group) {
if (in_array($customer_group['customer_group_id'], $this->config->get('config_customer_group_display'))) {
$data['customer_groups'][] = $customer_group;
}
}
}
$data['customer_group_id'] = $this->config->get('config_customer_group_id');
// Custom Fields
$data['custom_fields'] = [];
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields();
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
$data['custom_fields'][] = $custom_field;
}
}
// Captcha
$this->load->model('setting/extension');
$extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
$data['captcha'] = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code']);
} else {
$data['captcha'] = '';
}
$this->load->model('catalog/information');
$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
if ($information_info) {
$data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information.info', 'language=' . $this->config->get('config_language') . '&information_id=' . $this->config->get('config_account_id')), $information_info['title']);
} else {
$data['text_agree'] = '';
}
$data['language'] = $this->config->get('config_language');
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$this->response->setOutput($this->load->view('account/register', $data));
}
/**
* @return void
*/
public function register(): void {
$this->load->language('account/register');
$json = [];
$keys = [
'customer_group_id',
'firstname',
'lastname',
'email',
'telephone',
'custom_field',
'password',
'confirm',
'agree'
];
foreach ($keys as $key) {
if (!isset($this->request->post[$key])) {
$this->request->post[$key] = '';
}
}
if (!isset($this->request->get['register_token']) || !isset($this->session->data['register_token']) || ($this->session->data['register_token'] != $this->request->get['register_token'])) {
$json['redirect'] = $this->url->link('account/register', 'language=' . $this->config->get('config_language'), true);
}
if (!$json) {
// Customer Group
if ($this->request->post['customer_group_id']) {
$customer_group_id = (int)$this->request->post['customer_group_id'];
} else {
$customer_group_id = (int)$this->config->get('config_customer_group_id');
}
$this->load->model('account/customer_group');
$customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
if (!$customer_group_info || !in_array($customer_group_id, (array)$this->config->get('config_customer_group_display'))) {
$json['error']['warning'] = $this->language->get('error_customer_group');
}
if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
$json['error']['firstname'] = $this->language->get('error_firstname');
}
if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
$json['error']['lastname'] = $this->language->get('error_lastname');
}
if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
$json['error']['email'] = $this->language->get('error_email');
}
$this->load->model('account/customer');
if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
$json['error']['warning'] = $this->language->get('error_exists');
}
if ($this->config->get('config_telephone_required') && (oc_strlen($this->request->post['telephone']) < 3) || (oc_strlen($this->request->post['telephone']) > 32)) {
$json['error']['telephone'] = $this->language->get('error_telephone');
}
// Validate Address
if ((utf8_strlen(trim($this->request->post['address'])) < 1) || (utf8_strlen(trim($this->request->post['address'])) > 128)) {
$json['error']['address'] = 'Address must be between 1 and 128 characters!';
}
// Validate Phone
if ((utf8_strlen(trim($this->request->post['phone'])) < 3) || (utf8_strlen(trim($this->request->post['phone'])) > 32)) {
$json['error']['phone'] = 'Phone number must be between 3 and 32 characters!';
}
// Validate Date of Birth for age verification
$dob = new DateTime($this->request->post['dob']);
$today = new DateTime();
$age = $today->diff($dob)->y;
if ($age < 21) {
$json['error']['dob'] = 'You must be 21 years or older to register.';
}
// Custom field validation
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
}
}
}
if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 4) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
$json['error']['password'] = $this->language->get('error_password');
}
if ($this->request->post['password'] != $this->request->post['confirm']) {
$json['error']['confirm'] = $this->language->get('error_confirm');
}
// Captcha
$this->load->model('setting/extension');
$extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
$captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');
if ($captcha) {
$json['error']['captcha'] = $captcha;
}
}
// Agree to terms
$this->load->model('catalog/information');
$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
if ($information_info && !$this->request->post['agree']) {
$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
}
}
if (!$json) {
$this->load->model('account/customer');
$customer_id = $this->model_account_customer->addCustomer($this->request->post);
// Custom field validation
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
}
}
}
// Custom field validation
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'address') {
if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
}
}
}
// Validate password
if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 4) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
$json['error']['password'] = $this->language->get('error_password');
}
// Captcha
$this->load->model('setting/extension');
$extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
$captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');
if ($captcha) {
$json['error']['captcha'] = $captcha;
}
}
// Agree to terms
$this->load->model('catalog/information');
$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
if ($information_info && !$this->request->post['agree']) {
$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
}
// Add the customer
$customer_id = $this->model_account_customer->addCustomer($this->request->post);
// Login if no approval is required
$this->customer->login($this->request->post['email'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8'));
// Add to activity log
$this->load->model('account/activity');
$activity_data = [
'customer_id' => $customer_id,
'name' => $this->request->post['firstname'] . ' ' . $this->request->post['lastname']
];
$this->model_account_activity->addActivity('register', $activity_data);
$json['success'] = $this->language->get('text_success');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
?>
Code: Select all
$_['entry_address'] = 'Address';
$_['entry_phone'] = 'Phone';
$_['entry_dob'] = 'Date of Birth';
$_['error_address'] = 'Address must be between 1 and 128 characters!';
$_['error_phone'] = 'Phone number must be between 3 and 32 characters!';
$_['error_dob'] = 'You must be 21 years or older to register.';
Code: Select all
ALTER TABLE `customer` ADD `address` VARCHAR(128) NOT NULL;
ALTER TABLE `customer` ADD `phone` VARCHAR(32) NOT NULL;
ALTER TABLE `customer` ADD `dob` DATE NOT NULL;
<!-- Added these inside the registration form to make the needed fields -->
Code: Select all
<div class="row mb-3 required">
<label for="input-address" class="col-sm-2 col-form-label">{{ entry_address }}</label>
<div class="col-sm-10">
<input type="text" name="address" value="" placeholder="{{ entry_address }}" id="input-address" class="form-control"/>
<div id="error-address" class="invalid-address"></div>
</div>
</div>
Code: Select all
<div class="row mb-3 required">
<label for="input-phone" class="col-sm-2 col-form-label">{{ entry_phone }}</label>
<div class="col-sm-10">
<input type="text" name="phone" value="{{ phone }}" placeholder="{{ entry_phone }}" id="input-phone" class="form-control" />
<div class="text-danger"></div>
</div>
</div>
Code: Select all
<div class="row mb-3 required">
<label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
<div class="col-sm-10">
<input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control" />
<div class="text-danger"></div>
</div>
</div>
Anyway, I did a quick glance through your code and saw an error right away - you're using "utf8_strlen" when validating the "address" and "phone" fields. The function "utf8_strlen" is an old function used in OpenCart 3 and below. In OpenCart 4, it has changed to "oc_strlen" instead. You can see that in the validation code before the code you've written! They are clearly using "oc_strlen" instead of "utf8_strlen" - so try changing that up and follow the existing validation code.
Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
// THIS ISN'T WORKING YET
Code: Select all
$dob = new DateTime($this->request->post['dob']);
$today = new DateTime();
$age = $today->diff($dob)->y;
if ($age < 21) {
$json['error']['dob'] = $this->language->get('error_dob');
}
$json['age'] = $age; // Add age to the JSON response
error_log('User age: ' . $age); // This line is to print the age to the error log
If you're trying to write to your error logs, you should be using the following function:keithdarr wrote: ↑Sun Aug 04, 2024 1:39 pm// Validate Date of Birth for age verification
// THIS ISN'T WORKING YET
$dob = new DateTime($this->request->post['dob']);
$today = new DateTime();
$age = $today->diff($dob)->y;
if ($age < 21) {
$json['error']['dob'] = $this->language->get('error_dob');
}
$json['age'] = $age; // Add age to the JSON response
error_log('User age: ' . $age); // This line is to print the age to the error log
Code: Select all
$this->log->write('User age: ' . $age);
Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
I tried something a little more robust but, it still acts like no date is being entered. My date field is showing the date in the 08/04/2024 format. I don't know if that is messing with things. Just a thought that crossed my mind right now.
Code: Select all
// Validate Date of Birth for age verification
if (!empty($this->request->post['dob'])) {
try {
$dob = new DateTime($this->request->post['dob']);
$today = new DateTime();
$age = $today->diff($dob)->y;
if ($age < 21) {
$json['error']['dob'] = $this->language->get('error_dob');
}
// Add age to JSON response for display
$json['age'] = $age;
} catch (Exception $e) {
$json['error']['dob'] = 'Invalid date format';
}
} else {
$json['error']['dob'] = 'Date of Birth is required';
}
This is what my field looks like in catalog/view/template/account/register.twig
Code: Select all
<div class="row mb-3 required">
<label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
<div class="col-sm-10">
<input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control"/>
<div id="error-dob" class="invalid-feedback"></div>
<div id="age-display" class="text-info"></div>
<!-- This is the new div for displaying the age but this isn't working yet as the whole validation isn't working for the DOB -->
</div>
</div>
// I believe PHP expects YYYY-MM-DD
// the field is populated MM-DD-YYYY when the calendar is selected
// I put script at the end of catalog/view/template/account/register.twig
<!-- this script is to work with the DOB problem I have been having where the date is entered in MM/DD/YYYY and php expects YYYY/MM/DD
-->
Code: Select all
<script type="text/javascript">
$(document).ready(function() {
$('#form-register').on('submit', function(e) {
e.preventDefault();
// Reformat the date to YYYY-MM-DD
var dobInput = $('#input-dob');
var dobValue = dobInput.val();
var dateParts = dobValue.split('-'); // Assuming YYYY-MM-DD format
if (dateParts.length === 3) {
var formattedDate = dateParts[0] + '-' + dateParts[1] + '-' + dateParts[2];
dobInput.val(formattedDate);
} else {
alert('Invalid date format. Please enter the date in MM/DD/YYYY format.');
return;
}
$.ajax({
url: $(this).attr('action'),
type: 'post',
data: $(this).serialize(),
dataType: 'json',
beforeSend: function() {
// Clear all existing error messages and classes
$('.text-danger').remove();
$('.is-invalid').removeClass('is-invalid');
$('#age-display').text(''); // Clear age display
},
success: function(json) {
if (json['redirect']) {
location = json['redirect'];
} else if (json['success']) {
alert('Registration successful!');
} else {
if (json['error']) {
for (var i in json['error']) {
var element = $('#input-' + i.replace(/_/g, '-'));
if (element.parent().hasClass('input-group')) {
element.parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
} else {
element.after('<div class="text-danger">' + json['error'][i] + '</div>');
}
element.addClass('is-invalid');
}
}
if (json['age']) {
$('#age-display').text('Calculated Age: ' + json['age']);
}
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert('Error: ' + thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
});
</script>
Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig
Code: Select all
$date1 = new DateTime("2024-08-04");
$date2 = new DateTime("08/04/2024");
$date3 = new DateTime("4th August 2024");
Code: Select all
<div class="col mb-3 required">
<label for="input-dob" class="form-label">{{ entry_dob }}</label>
<div class="input-group">
<input type="text" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control date" />
<div class="input-group-text"><i class="fa-regular fa-calendar"></i></div>
</div>
<div id="error-dob" class="invalid-feedback"></div>
</div>
Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
[ code] and [ /code]
without spaces.
UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk

Users browsing this forum: No registered users and 5 guests