Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

How to implement Laravel password validation rules as string in arrays in laravel ?

By M.K. Verma · 2 May 2022

How to implement Laravel password validation rules as string in arrays in laravel ?

Try 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)