Home > OS >  How to implement Laravel password validation rules as string in arrays?
How to implement Laravel password validation rules as string in arrays?

Time:04-30

I want to use the out-of-the-box Laravel password validation rules. This is possible by using the use Illuminate\Validation\Rules\Password class.

So you can use those rules like so:

\Illuminate\Validation\Rule\Password::min(8)->letters()->numbers()->mixedCase()->uncompromised(3)

However, you can't use those rules inside a config file, like in this package because you get the following error when running

artisan config:cache

❯ artisan config:cache > ─╯ Configuration cache cleared successfully.

LogicException

Your configuration files are not serializable.

at vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php:84 80▕ require $configPath; 81▕ } catch (Throwable $e) { 82▕ $this->files->delete($configPath); 83▕ ➜ 84▕ throw new LogicException('Your configuration files are not serializable.', 0, $e); 85▕ } 86▕ 87▕ $this->info('Configuration cached successfully.'); 88▕ }

1 bootstrap/cache/config.php:859 Error::("Call to undefined method Illuminate\Validation\Rules\Password::__set_state()")

After reading the docs, it seems that you can't use those password rules as strings inside an array like so:

"password_rules" => ['min:8'],

So checking the available rules, the rest of the following rules (letters, mixedCase, uncompromised) are not available.

Then, is there a workaround to set those password rules inside the config file? (so that, I can avoid the LogicException: Your configuration files are not serializable. after executing the artisan config:cache command).

CodePudding user response:

use Illuminate\Validation\Rules\Password;

$rules = [
    'password' => [
        'required',
        'string',
        Password::min(8)
            ->mixedCase()
            ->numbers()
            ->symbols()
            ->uncompromised(),
        'confirmed'
    ],
]

Try using the above code it should work.

CodePudding user response:

I am not a Laravel person, but you can put the config into an array and then unpack it later in your app with something like this:

// for illustration
class Mock {
    public function __call($name, $args) {
        printf("called: %s(%s)\n", $name, implode(', ', $args));
        return $this;
    }
    
    // this is awful. never do this.
    // in Laravel the min() function, and only this function, is basically
    // an alias to the constructor, which is weird.
    public static function __callStatic($name, $args) {
        return (new self())->$name(...$args);
    }
}

$params = [
    'min' => [8],
    'letters' => [],
    'numbers' => [],
    'mixedCase' => [],
    'uncompromised' => [3]
];

$o = NULL;
foreach( $params as $func => $args ) {
    if( is_null($o) ) {
        $o = Mock::$func(...$args);
    } else {
        $o = $o->$func(...$args);
    }
}

Output:

called: min(8)
called: letters()
called: numbers()
called: mixedCase()
called: uncompromised(3)
  • Related