Files
breadcrumb-the-shire/templates/partials/app-details-validation-summary.phtml
2026-03-04 15:56:58 +01:00

168 lines
5.3 KiB
PHTML

<?php
/**
* Shared validation summary for detail pages.
*
* Supported inputs:
* - $validationSummaryErrors (preferred) or fallback $errors
* - $validationSummaryTitle (optional)
* - $validationSummaryAutoFocus (optional, default true)
*
* Error item formats:
* - string message
* - ['message' => '...', 'fieldName' => 'email', 'fieldId' => 'email']
* - FormErrors::toArray()-like map: ['email' => ['Required']]
*/
$validationSummaryErrorsRaw = $validationSummaryErrors ?? ($errors ?? []);
if (!is_array($validationSummaryErrorsRaw)) {
$validationSummaryErrorsRaw = [];
}
$validationSummaryTitle = isset($validationSummaryTitle)
? trim((string) $validationSummaryTitle)
: t('Please review the following errors');
$validationSummaryAutoFocus = !isset($validationSummaryAutoFocus) ? true : !empty($validationSummaryAutoFocus);
$validationSummaryItems = [];
$pushValidationSummaryItem = static function (
array &$items,
string $message,
string $fieldName = '',
string $fieldId = '',
string $href = ''
): void {
$message = trim($message);
if ($message === '') {
return;
}
$items[] = [
'message' => $message,
'fieldName' => trim($fieldName),
'fieldId' => trim($fieldId),
'href' => trim($href),
];
};
foreach ($validationSummaryErrorsRaw as $errorKey => $entry) {
$keyFieldName = is_string($errorKey) ? trim($errorKey) : '';
if ($keyFieldName === 'input') {
$keyFieldName = '';
}
if (is_array($entry)) {
if (array_is_list($entry)) {
foreach ($entry as $listMessage) {
if (!is_scalar($listMessage)) {
continue;
}
$pushValidationSummaryItem(
$validationSummaryItems,
(string) $listMessage,
$keyFieldName
);
}
continue;
}
$message = '';
if (isset($entry['message'])) {
$message = (string) $entry['message'];
} elseif (isset($entry['text'])) {
$message = (string) $entry['text'];
} elseif (isset($entry['label'])) {
$message = (string) $entry['label'];
} else {
foreach ($entry as $candidate) {
if (is_scalar($candidate)) {
$message = (string) $candidate;
break;
}
}
}
$fieldName = '';
if (isset($entry['fieldName'])) {
$fieldName = (string) $entry['fieldName'];
} elseif (isset($entry['field_name'])) {
$fieldName = (string) $entry['field_name'];
} elseif (isset($entry['field'])) {
$fieldName = (string) $entry['field'];
} elseif (isset($entry['name'])) {
$fieldName = (string) $entry['name'];
} elseif ($keyFieldName !== '') {
$fieldName = $keyFieldName;
}
if (trim($fieldName) === 'input') {
$fieldName = '';
}
$fieldId = isset($entry['fieldId'])
? (string) $entry['fieldId']
: (string) ($entry['field_id'] ?? '');
$href = (string) ($entry['href'] ?? '');
$pushValidationSummaryItem($validationSummaryItems, $message, $fieldName, $fieldId, $href);
continue;
}
if (is_scalar($entry)) {
$pushValidationSummaryItem(
$validationSummaryItems,
(string) $entry,
$keyFieldName
);
}
}
if (!$validationSummaryItems) {
return;
}
?>
<div class="app-details-errors">
<section
class="app-details-validation-summary"
role="alert"
aria-live="assertive"
data-validation-summary
data-validation-summary-autofocus="<?php e($validationSummaryAutoFocus ? '1' : '0'); ?>"
tabindex="-1"
>
<?php if ($validationSummaryTitle !== ''): ?>
<p class="app-details-validation-summary-title"><?php e($validationSummaryTitle); ?></p>
<?php endif; ?>
<ul class="app-details-validation-summary-list">
<?php foreach ($validationSummaryItems as $item): ?>
<?php
$message = (string) ($item['message'] ?? '');
$fieldName = (string) ($item['fieldName'] ?? '');
$fieldId = (string) ($item['fieldId'] ?? '');
$href = (string) ($item['href'] ?? '');
$hasFieldTarget = $fieldId !== '' || $fieldName !== '';
$targetHref = $href;
if ($targetHref === '' && $fieldId !== '') {
$targetHref = '#' . $fieldId;
}
if ($targetHref === '' && $fieldName !== '') {
$targetHref = '#';
}
?>
<li>
<?php if ($targetHref !== ''): ?>
<a
class="app-details-validation-summary-link"
href="<?php e($targetHref); ?>"
<?php if ($hasFieldTarget): ?>data-validation-error-link<?php endif; ?>
<?php if ($fieldId !== ''): ?>data-validation-target-id="<?php e($fieldId); ?>"<?php endif; ?>
<?php if ($fieldName !== ''): ?>data-validation-target-name="<?php e($fieldName); ?>"<?php endif; ?>
>
<?php e($message); ?>
</a>
<?php else: ?>
<?php e($message); ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</section>
</div>