Respect\Validation custom Rule with PDO?

914 views Asked by At

I am learning Slim Framework v4 and decided to use Respect\Validation to validate inputted data and have hit a snag where I do not know how to inject the PDO into my custom rule I created.

The idea is to validate some inputs against the database if the provided data exist (or in another instances, if it was inputted correctly). In this specific case, I am tying to validate user's credentials for log in. My idea is this:

AuthController.php:

    v::with('app\\Validators\\');
    $userValidation = v::notBlank()->email()->length(null, 255)->EmailExists()->setName('email');

EmailExists() is my custom rule.

EmailExists.php:

namespace app\Validators;

use PDO;
use Respect\Validation\Rules\AbstractRule;

class EmailExists extends AbstractRule
{
    protected $pdo;

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function validate($input, $id = null)
    {
    // a PDO query that checks if the email exists in database
    }
}

But I get an error of Too few arguments to function app\Validators\EmailExists::__construct(), 0 passed and exactly 1 expected, which is somewhat expected since the AbstractRule does not have a PDO injected and my class extends it.

So how to inject the PDO interface so that I can use it in my custom rules?

Are you guys using another approach in validating this kind of data? Do note that I am writing an API, so the database validation is somewhat a must and after Googling for past two days, I have no solutions at hand.

I am also using a PHP-DI where I create PDO interface. This is my dependencies.php file:

declare(strict_types=1);

use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use app\Handlers\SessionMiddleware;

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        PDO::class => function (ContainerInterface $c) {
            $settings = $c->get('settings')['db'];

            $db = new PDO("mysql:host={$settings['host']};dbname={$settings['database']};charset={$settings['charset']},{$settings['username']},{$settings['password']}");
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8',time_zone='{$offset}'");
            $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

            return $db;
        },
        'session' => function(ContainerInterface $c) {
            return new SessionMiddleware;
        }
    ]);
};

And (part of) index.php:

declare(strict_types=1);

use DI\ContainerBuilder;
use Slim\Factory\AppFactory;

// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();

// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);

// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);

// Build PHP-DI Container instance
$container = $containerBuilder->build();

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();

// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);

// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);

// Add Routing Middleware
$app->addRoutingMiddleware();

// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);

Any help would be appreciated.

2

There are 2 answers

1
czk On
class EmailAvailable extends AbstractRule {

    /**
     * @param $input
     *
     * @return bool
     */
    public function validate ($sInput) {
        return User::where('user_email', $sInput)->count() === 0;
    }
}
0
czk On

Use your user model to count the number of rows in the user table where there is a hit. If it is not exactly 0, the check returns false, if it is exactly 0, the check passes. So you don't have to include a PDO at this point. I use Slim 3 and that works quite well.

namespace app\Validators;

use Respect\Validation\Rules\AbstractRule;

class EmailAvailable extends AbstractRule {

        /**
         * @param $input
         *
         * @return bool
         */
        public function validate ($sInput) {
            return User::where('user_email', $sInput)->count() === 0;
        }
    }