我正在尝试在客户的(admin->customer->tab general)客户部分添加一个新字段(以编程方式),称为客户编号。一切都按预期工作,但我不想在数据库中获得双数,因为用户可以输入自己的号码。每次从管理员创建新用户时,代码都会查找最后一个(最高)客户编号并为其添加 +1。
我找不到这种情况的解决方案:
如果客户已经存在(编辑模式),它应该显示一个不可编辑的值(只读)
如果注册新客户,则应显示最后(最高)客户编号(可编辑)。如果用户输入的数字低于最高客户编号,则应显示错误,并应显示最高客户编号 + 1。
这是我到目前为止在控制器(getForm)中的代码:
if (isset($this->error['customer_number'])) {
$data['error_customer_number'] = $this->error['customer_number'];
} else {
$data['error_customer_number'] = '';
}
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number'])) {
$data['customer_number'] = $this->request->post['customer_number'];
} elseif (!empty($customer_info)) {
$data['customer_number'] = $customer_info['customer_number'];
} else {
$data['customer_number'] = '0';
}
在验证(validateForm)中:
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number']) && ($this->request->post['customer_number'] <= $data['latest_customer'])) {
$this->error['customer_number'] = $this->language->get('error_customer_number');
}
在我的 customer_form.tpl 我有:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-customer-number"><span data-toggle="tooltip" title="<?php echo $help_customer_number; ?>"> <?php echo $entry_customer_number; ?></span></label>
<div class="col-sm-10">
<?php if (!empty($customer_number) || $customer_number != 0){ ?>
<input type="text" name="customer_number" value="<?php echo $customer_number; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php }else{ ?>
<input type="text" name="customer_number" value="<?php echo $latest_customer + 1; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php if ($error_customer_number) { ?>
<div class="text-danger"><?php echo $error_customer_number; ?></div>
<?php } ?>
<?php } ?>
</div>
</div>
这显然不起作用,因为在编辑现有客户时,客户编号始终低于系统中的最高客户编号(因此会出错)。希望有人可以帮助我解决这个问题!提前致谢!