Skip to content

Commit

Permalink
Show custom error messages for invalid input.
Browse files Browse the repository at this point in the history
Closes #8.
  • Loading branch information
franzliedke committed Mar 17, 2015
1 parent efb79cc commit 47e0c13
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Parts/Composer/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public function setupPackage($composer, Filesystem $target)
// Ask for package name
$composer->name = $this->input->ask(
'Please name this package',
'/[[:alnum:]]+\/[[:alnum:]]+/'
'/[[:alnum:]]+\/[[:alnum:]]+/',
'Please enter a valid package name in the format "vendor/name".'
);

// Ask for the root namespace
$namespace = $this->input->ask(
'Please provide a default namespace (PSR-4)',
'/([[:alnum:]]+\\\\?)+/',
'Please enter a valid PHP namespace',
$this->makeDefaultNamespace($composer->name)
);

Expand Down
12 changes: 7 additions & 5 deletions src/Parts/ConsoleInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ public function confirm($question, $default = false)
);
}

public function ask($question, $regex, $default = null)
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),
$this->validateWith($regex, $errorText),
false,
$default
);
}

protected function validateWith($regex)
protected function validateWith($regex, $errorText)
{
return function ($answer) use ($regex) {
if (!$errorText) $errorText = 'Invalid. Please try again.';

return function ($answer) use ($regex, $errorText) {
if (preg_match($regex, $answer)) return $answer;

throw new \RuntimeException('Invalid. Try again.');
throw new \RuntimeException($errorText);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parts/PartInputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ interface PartInputInterface

public function confirm($question);

public function ask($question, $regex, $default = null);
public function ask($question, $regex, $errorText = null, $default = null);

}

0 comments on commit 47e0c13

Please sign in to comment.