public askAndValidate ( Symfony\Component\Console\Output\OutputInterface $output, string | array $question, callback $validator, integer $attempts = false, string $default = null ) : mixed | ||
$output | Symfony\Component\Console\Output\OutputInterface | |
$question | string | array | |
$validator | callback | A PHP callback |
$attempts | integer | Max number of times to ask before giving up (false by default, which means infinite) |
$default | string | The default answer if none is given by the user |
return | mixed |
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
{
$error = null;
while (false === $attempts || $attempts--) {
if (null !== $error) {
$output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
$value = $this->ask($output, $question, $default);
try {
return call_user_func($validator, $value);
} catch (\Exception $error) {
}
}
throw $error;
}
public function ask($question, $regex, $errorText = null, $default = null) { if ($default) { $question = "{$question} [{$default}]"; } return $this->dialog->askAndValidate($this->output, "<question>{$question}</question> ", $this->validateWith($regex, $errorText), false, $default); }