Skip to content

Commit 91ee0d1

Browse files
committed
Implement required fields in a simple way
1 parent f7e7096 commit 91ee0d1

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Form/Field/Field.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ public function getBlock(): AbstractBlock
6767

6868
public function getFieldAttributes(): array
6969
{
70-
return (array)$this->getData('field_attributes');
70+
$fieldAttributes = (array)$this->getData('field_attributes');
71+
72+
if ($this->isRequired()) {
73+
$fieldAttributes['required'] = null;
74+
}
75+
76+
return $fieldAttributes;
7177
}
7278

7379
public function getLabelAttributes(): array

view/adminhtml/templates/form/field.phtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ if (!empty($depends)) {
4444
<div class="admin__field-label">
4545
<label for="<?= $escaper->escapeHtml($field->getCode()) ?>">
4646
<span><?= $escaper->escapeHtml($field->getLabel()) ?></span>
47+
<?php if ($field->isRequired()): ?>
48+
<span style="color:red">*</span>
49+
<?php endif; ?>
4750
</label>
4851
</div>
4952

view/adminhtml/templates/form/field_type/input.phtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ if (isset($fieldAttributes['type'])) {
3030
name="<?= $escaper->escapeHtml($field->getCode()) ?>"
3131
data-name="<?= $escaper->escapeHtml($field->getCode()) ?>"
3232
<?php foreach ($fieldAttributes as $attributeName => $attributeValue) : ?>
33-
<?= /* @noEscape */ $attributeName ?>="<?= /* @noEscape */ $attributeValue ?>"
33+
<?php if ($attributeValue === null): ?>
34+
<?= /* @noEscape */ $attributeName ?>
35+
<?php else: ?>
36+
<?= /* @noEscape */ $attributeName ?>="<?= /* @noEscape */ $attributeValue ?>"
37+
<?php endif; ?>
3438
<?php endforeach; ?>
3539
:value="<?= $escaper->escapeHtml($field->getScope()) ?>.<?= $escaper->escapeHtml($field->getCode()) ?>"
3640
@change="<?= $escaper->escapeHtml($field->getAlpineSetter()) ?>"

0 commit comments

Comments
 (0)