嗨,我想对Opencart 2.3.0.2上的特定自定义字段进行验证。在我的代码上,我必须禁用默认验证,因此我只需要对表单上的一个自定义字段进行验证。名为“ NUMBER”的自定义字段是客户地址的数量。验证目的是检查该字段是否为空。所以我正在做这个
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
foreach ($custom_fields as $custom_field) {
if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id'] == 7])) {$json['error']['custom_field' . $custom_field['custom_field_id'] == 7] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);}}
但是当我提交表格时,它没有显示任何错误或带有文本危险的div。任何人都可以帮助此代码。我很感激
opencart在数组中有一个默认代码,可验证所有字段。我所做的是删除此验证配置,仅进行一个字段。因此,位于结帐/结帐处的custom字段的默认验证-shiping_address表单为
foreach ($custom_fields as $custom_field) {
if (($custom_field['location'] == 'address') && $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['location'] == 'address') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/' . html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8') . '/')))) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
}
我注释了此代码并进行了修改
foreach ($custom_fields as $custom_field) {
if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
//number field
if($custom_field['custom_field_id'] == 7) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
} elseif (($custom_field['location'] == 'address') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/' . html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8') . '/')))) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
}
我只是进行条件检查,如果custom_field 7为空,则显示错误
答案 0 :(得分:0)
您需要分离条件逻辑。现在,您已经压缩了语法并破坏了逻辑。
empty($this->request->post['custom_field'][$custom_field['custom_field_id'] == 7])
// this is performing an evaluation -------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// returning a true or false value
// if false, the key is converted to 0 because false isn't a valid key
// if true, the key is converted to 1
麻烦的是您在$this->request->post['custom_field']
中没有这些数字键,因此它们会使empty()
返回true
。
改用它:
if ($custom_field['location'] == 'address'
&& $custom_field['required']
&& // iterate your post array and check $post['custom_field_id'] == 7
&& // iterate your post array and check empty($post['custom_field_value'])) {