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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?