[10.x] Add support to ask and validate input in console commands #47813
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When running commands that require a lot of input from the user, if you get partway through and the input is incorrect in some way, you may have to run the command again (depending on how the command is written to handle bad input).
It would be nice to ask for input and validate in a loop until the value is correct. The resulting code makes commands easy to read by combining the input and checking logic.
Example code below:
You can even override messages too:
In the askAndValidate method the user will be prompted in a loop until the validation passes. The Validator facade is used to make and validate the input from the user.
Caveats
There is 1 caveat with the validation part, as thats when adding custom error messages, the field name can only be
input
. Not sure of an alternative to tackle this.