I am creating a command to generate accounts from a file. In command I have passed some arguments.
$this
->setName('batch:create')
->setDescription('xyz')
->setHelp('xyz')
->addArgument('account-id', InputArgument::REQUIRED, "Set the account id.")
->addArgument('name', InputArgument::REQUIRED, "Set the account name.");
I was just thinking if there is any way I can check type of argument passed. For now I am checking it like this,
if (is_numeric($input->getArgument('account-id'))) {
// ....
}
Is there anyway I can create a validator that checks the type and I just have to call validate function.
if ($input->validate() === false) {
// show error message and return.
}
Unfortunately, currently there's no way to implement command argument validation in Symfony. The best way to implement these checks would be overriding Symfony\Component\Console\Command::initialize
method in your command and then applying the validation rules there, throwing exceptions if passed arguments are invalid.
Update: Matthias Noback has implemented symfony-console-form
(https://github.com/matthiasnoback/symfony-console-form), and looks like implementing Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand
interface would give you basic validation abilities through the form component (have to test it with validation, though).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With