如何在 Opencart 上仅对一个自定义字段进行验证 - 已解决
How to make a Validation for only one custom field on Opencart - resolved
您好,我想对 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 在数组中有一个默认代码,用于验证所有字段。我所做的是删除此验证配置并仅创建一个字段。因此,位于 checkout/checkout - 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,如果为空,显示错误
您需要分离条件逻辑。现在你已经压缩了你的语法并破坏了逻辑。
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()
return 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'])) {
您好,我想对 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 在数组中有一个默认代码,用于验证所有字段。我所做的是删除此验证配置并仅创建一个字段。因此,位于 checkout/checkout - 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,如果为空,显示错误
您需要分离条件逻辑。现在你已经压缩了你的语法并破坏了逻辑。
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()
return 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'])) {