dongqi8030
2019-07-03 12:26
浏览 40

如何在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'])) {
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题