dongqi8030 2019-07-03 12:26
浏览 42
已采纳

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

Hi i'd like to make a validation for a specific custom field on Opencart 2.3.0.2. On my code i had to disable the default validation, so i need to make a validation for only one custom field on form. The custom field called "NUMBER", is the number of customer's address. The validation purpose is check if the field is empty or not. So i'm making this

$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']);}} 

But when i submit the form its not showing any error or the div with text danger. Can anyone helps with this code. I'm grateful

The opencart has a default code in array who validate all the field. What i did was delete this validate configuration and make for only one field. So, the default validation for custom field located on checkout/checkout - shiping_address form is,

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']);
                }
            }

i commented this code and make this modification

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']);
                }
            }

i just make a condition checking if is custom_field 7, if empty, shows the error

  • 写回答

1条回答 默认 最新

  • doupang4126 2019-07-03 13:03
    关注

    You need to separate the condition logic. Right now you have compacted your syntax and damaged the logic.

    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
    

    The trouble is you don't have those numeric keys in $this->request->post['custom_field'], so they will make empty() return true. Use this instead:

    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'])) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择