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