29 lines
1.1 KiB
PHP
29 lines
1.1 KiB
PHP
<?php
|
|
$errors = [];
|
|
foreach (['name', 'phone', 'age', 'consent', 'guard', 'save'] as $field) {
|
|
$message = trim(strip_tags((string) $modx->getPlaceholder('fi.error.' . $field)));
|
|
if ($message !== '') {
|
|
$errors[$field] = $message;
|
|
}
|
|
}
|
|
$success = (string) $modx->getPlaceholder('fi.success') === '1' && $errors === [];
|
|
$general = (string) $modx->getPlaceholder('fi.validation_error_message');
|
|
|
|
$body = ['ok' => $success];
|
|
if (!$success) {
|
|
$field = array_key_first($errors) ?? '';
|
|
$body['field'] = in_array($field, ['name', 'phone', 'age', 'consent'], true) ? $field : '';
|
|
$body['message'] = $field === 'guard' ? $modx->lexicon('ls_err_guard_retry') : ($errors[$field] ?? $modx->lexicon('ls_err_save'));
|
|
$body['code'] = $field === 'guard' ? ($errors['guard'] ?? 'guard') : '';
|
|
}
|
|
|
|
while (ob_get_level()) {
|
|
ob_end_clean();
|
|
}
|
|
http_response_code($success ? 200 : 422);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Cache-Control: no-store');
|
|
header('X-Robots-Tag: noindex');
|
|
echo json_encode($body, JSON_UNESCAPED_UNICODE);
|
|
exit;
|