Symfony\Component\Console\Helper\DialogHelper::askAndValidate PHP 메소드

askAndValidate() 공개 메소드

The validator receives the data to validate. It must return the validated data when the data is valid and throw an exception otherwise.
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
리턴 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;
    }

Usage Example

예제 #1
0
 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);
 }
All Usage Examples Of Symfony\Component\Console\Helper\DialogHelper::askAndValidate