Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate console command prompt inputs

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.

enter image description here

Thanks in advance.

like image 425
Saumini Navaratnam Avatar asked Mar 25 '26 19:03

Saumini Navaratnam


2 Answers

Below is another way you can do it using Laravel's Validator.

My custom method validate_cmd takes 2 parameters:

  1. A Command method (ask, secret, choice...) as an anonymous function
  2. An array of the rules

If 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;
        }

    }

}
like image 151
Stubbies Avatar answered Mar 28 '26 13:03

Stubbies


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();
    }

}
like image 29
linktoahref Avatar answered Mar 28 '26 13:03

linktoahref



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!