Как сделать проверку только для одного настраиваемого поля в Opencart - решено - PullRequest
0 голосов
/ 03 июля 2019

Привет, я хотел бы сделать проверку для определенного настраиваемого поля в 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']);
                }
            }

Я просто проверил условиеif is custom_field 7, если пусто, показывает ошибку

1 Ответ

0 голосов
/ 03 июля 2019

Вам необходимо отделить логику условий.Прямо сейчас вы сжали свой синтаксис и повредили логику.

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'])) {
...