Following console command I'm prompting for input. I would like to validate the user input. How can I do that?
I have check the this plugin. But it looks like validating arguments & options. Not the interactive inputs.
CreateClient.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CreateClient extends Command {
...
public function handle() {
$name = $this->ask('Enter name ');
$email = $this->ask('Enter email ');
$date = $this->ask('Enter date [Eg: 2016-01-01 00:00:00] ');
// Validate user input
$this->info('Validating user inputs');
// How to validate email & date format ?
}
}
Update
Yes I'm aware of Validator, but the concern is the validation stops the user journey. User have restart from the beginning. Is it possible to attach a validation to each question, so the validation will be immediate and prompt to enter again.
Below is something I notice when I doesn't enter any value, I'm wondering how can I provide more validation rule.

Thanks in advance.
Below is another way you can do it using Laravel's Validator.
My custom method validate_cmd takes 2 parameters:
Command method (ask, secret, choice...) as an anonymous functionIf validation fails the Command method will be called again.
Of course you would want to place this in a Trait.
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Validator;
class CreateClient extends Command {
public function handle() {
$name = $this->validate_cmd(function() {
return $this->ask('Enter name');
}, ['name','required']);
$email = $this->validate_cmd(function() {
return $this->ask('Enter email');
}, ['email','required|email']);
$date = $this->validate_cmd(function() {
return $this->ask('Enter date [Eg: 2016-01-01 00:00:00]');
}, ['date','required']);
}
/**
* Validate an input.
*
* @param mixed $method
* @param array $rules
* @return string
*/
public function validate_cmd($method, $rules)
{
$value = $method();
$validate = $this->validateInput($rules, $value);
if ($validate !== true) {
$this->warn($validate);
$value = $this->validate_cmd($method, $rules);
}
return $value;
}
public function validateInput($rules, $value)
{
$validator = Validator::make([$rules[0] => $value], [ $rules[0] => $rules[1] ]);
if ($validator->fails()) {
$error = $validator->errors();
return $error->first($rules[0]);
}else{
return true;
}
}
}
One way would be to use FILTER_VALIDATE_EMAIL and Carbon
public function handle() {
$name = $this->ask('Enter name ');
$email = $this->ask('Enter email ');
$date = $this->ask('Enter date [Eg: 2016-01-01 00:00:00] ');
// Validate user input
$this->info('Validating user inputs');
// How to validate email & date format ?
$email_status = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email_status) {
// Invalid EMAIL
}
if (\Carbon::createFromFormat('YOUR DATE FORMAT', $date) === false) {
// Invalid date
}
}
Another method would be as said by @btl using Validator
public function handle() {
$name = $this->ask('Enter name ');
$email = $this->ask('Enter email ');
$date = $this->ask('Enter date [Eg: 2016-01-01 00:00:00] ');
// Validate user input
$this->info('Validating user inputs');
$data = array(
'email' => $email,
'date' => $date
);
$rules = array(
'email' => 'required|email',
'date' => 'required|date',
);
$validator = \Validator::make($data, $rules);
if ($validator->fails()) {
$messages = $validator->messages();
}
}
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