Skip to content

Commit 27779f8

Browse files
committed
Update password ask example to illustrate custom validation message
1 parent 70bf6e5 commit 27779f8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

examples/input-password.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
$itemCallable = function (CliMenu $menu) {
99
$result = $menu->askPassword()
1010
->setPlaceholderText('')
11+
->setValidator(function ($password) {
12+
if ($password === 'password') {
13+
$this->setValidationFailedText('Password is too weak');
14+
return false;
15+
} else if (strlen($password) <= 6) {
16+
$this->setValidationFailedText('Password is not long enough');
17+
return false;
18+
}
19+
20+
return true;
21+
})
1122
->ask();
1223

1324
var_dump($result->fetch());

src/Input/Password.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ public function getPlaceholderText() : string
8181
return $this->placeholderText;
8282
}
8383

84-
public function setValidator(callable $validator) : void
84+
public function setValidator(callable $validator) : Input
8585
{
8686
$this->validator = $validator;
87+
88+
return $this;
8789
}
8890

8991
public function ask() : InputResult

0 commit comments

Comments
 (0)