Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Rule/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\DomValidation\Rule;

use Gt\Dom\Element;
use Gt\Dom\ElementType;

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

public function getHint(Element $element, string $value):string {
if($element->elementType === ElementType::HTMLSelectElement) {
return "Please select an item in the list";
}

if($element->elementType === ElementType::HTMLInputElement
&& $element->type === "radio") {
return "Please select one of these options";
}

return "This field is required";
}
}
4 changes: 2 additions & 2 deletions test/phpunit/Rule/SelectElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testSelectMissingRequired():void {
$errorArray = iterator_to_array($validator->getLastErrorList());
self::assertCount(1, $errorArray);
self::assertSame(
"This field is required",
"Please select an item in the list",
$errorArray["currency"]
);
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testSelectTwoInvalidOptionsAndOneMissing():void {
$errorArray = iterator_to_array($validator->getLastErrorList());
self::assertCount(3, $errorArray);
self::assertSame(
"This field is required",
"Please select an item in the list",
$errorArray["currency"]
);
self::assertSame(
Expand Down
2 changes: 1 addition & 1 deletion test/phpunit/Rule/TypeRadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testRadioMissingRequired():void {
catch(ValidationException) {
$errorArray = iterator_to_array($validator->getLastErrorList());
self::assertSame(
"This field is required",
"Please select one of these options",
$errorArray["currency"],
);
}
Expand Down
Loading