Skip to content

Commit 15755de

Browse files
committed
Add ARIA attributes for forward compatibility with Cake 4.3+.
1 parent 51eb0cb commit 15755de

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/View/Helper/FormHelper.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ public function control(string $fieldName, array $options = []): string
482482

483483
$options = $this->_containerOptions($fieldName, $options);
484484
$options = $this->_feedbackStyleOptions($fieldName, $options);
485+
$options = $this->_ariaOptions($fieldName, $options);
485486
$options = $this->_helpOptions($fieldName, $options);
486487
$options = $this->_tooltipOptions($fieldName, $options);
487488

@@ -929,6 +930,53 @@ protected function _feedbackStyleOptions(string $fieldName, array $options): arr
929930
return $options;
930931
}
931932

933+
/**
934+
* Modify options for aria attributes.
935+
*
936+
* `aria-invalid` and `aria-required` are injected for forwards
937+
* compatibility reasons, as they have been introduced in the core form
938+
* helper with CakePHP 4.3. This can be removed once the required minimum
939+
* CakePHP version is bumped accordingly.
940+
*
941+
* @param string $fieldName Field name.
942+
* @param array<string, mixed> $options Options. See `$options` argument of `control()` method.
943+
* @return array<string, mixed>
944+
*/
945+
protected function _ariaOptions(string $fieldName, array $options): array
946+
{
947+
if (
948+
$options['type'] === 'hidden' ||
949+
$options['type'] === 'select' ||
950+
isset($options['multiple']) ||
951+
(
952+
isset($options['aria-required']) &&
953+
isset($options['aria-invalid'])
954+
)
955+
) {
956+
return $options;
957+
}
958+
959+
$isError =
960+
$options['error'] !== false &&
961+
$this->isFieldError($fieldName);
962+
963+
if (
964+
$isError &&
965+
!isset($options['aria-invalid'])
966+
) {
967+
$options['aria-invalid'] = 'true';
968+
}
969+
970+
if (
971+
$options['required'] &&
972+
!isset($options['aria-required'])
973+
) {
974+
$options['aria-required'] = 'true';
975+
}
976+
977+
return $options;
978+
}
979+
932980
/**
933981
* Modify options for control's help.
934982
*

0 commit comments

Comments
 (0)