Skip to content

Commit 7241c0c

Browse files
authored
feature: differentiate select/radio errors (#53)
closes #21
1 parent fc5b8e6 commit 7241c0c

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/Rule/Required.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Gt\DomValidation\Rule;
33

44
use Gt\Dom\Element;
5+
use Gt\Dom\ElementType;
56

67
class Required extends Rule {
78
protected array $attributes = [
@@ -13,6 +14,15 @@ public function isValid(Element $element, string|array $value, array $inputKvp):
1314
}
1415

1516
public function getHint(Element $element, string $value):string {
17+
if($element->elementType === ElementType::HTMLSelectElement) {
18+
return "Please select an item in the list";
19+
}
20+
21+
if($element->elementType === ElementType::HTMLInputElement
22+
&& $element->type === "radio") {
23+
return "Please select one of these options";
24+
}
25+
1626
return "This field is required";
1727
}
1828
}

test/phpunit/Rule/SelectElementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testSelectMissingRequired():void {
4040
$errorArray = iterator_to_array($validator->getLastErrorList());
4141
self::assertCount(1, $errorArray);
4242
self::assertSame(
43-
"This field is required",
43+
"Please select an item in the list",
4444
$errorArray["currency"]
4545
);
4646
}
@@ -142,7 +142,7 @@ public function testSelectTwoInvalidOptionsAndOneMissing():void {
142142
$errorArray = iterator_to_array($validator->getLastErrorList());
143143
self::assertCount(3, $errorArray);
144144
self::assertSame(
145-
"This field is required",
145+
"Please select an item in the list",
146146
$errorArray["currency"]
147147
);
148148
self::assertSame(

test/phpunit/Rule/TypeRadioTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testRadioMissingRequired():void {
4242
catch(ValidationException) {
4343
$errorArray = iterator_to_array($validator->getLastErrorList());
4444
self::assertSame(
45-
"This field is required",
45+
"Please select one of these options",
4646
$errorArray["currency"],
4747
);
4848
}

0 commit comments

Comments
 (0)