如何仅在OpenCart上的一个自定义字段进行验证 - 已解决



嗨,我想在OpenCart 2.3.0.2上对特定自定义字段进行验证。在我的代码上,我必须禁用默认验证,因此我只需要对表单上的一个自定义字段进行验证。称为"数字"的自定义字段是客户地址的数量。验证目的是检查字段是否为空。所以我做这个

$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()返回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'])) {

最新更新