This is what I have just used in an artisan 5.0 console command, to validate the arguments. This is in the fire()
method:
// Create the validator.
$validator = Validator::make(
$this->argument(),
['field1' => 'required|other|rules']
);
// Validate the arguments.
if ($validator->fails())
{
// Failed validation.
// Each failed field will have one or more messages.
foreach($validator->messages()->getMessages() as $field_name => $messages) {
// Go through each message for this field.
foreach($messages AS $message) {
$this->error($field_name . ': ' . $message);
}
}
// Indicate the command has failed.
return 1;
}
This is an extension on the answer from @tfont
You may need to change where the message is sent ($this->error()
) if this command is not being run as an console command, ie CLI, command-line.