Respect \ Validation自定义规则与PDO?

问题描述 投票:1回答:1

我正在学习Slim Framework v4,并决定使用Respect\Validation来验证输入的数据,并且遇到了一个障碍,我不知道如何将PDO注入我创建的自定义规则中。

想法是,如果提供的数据存在(或在其他情况下,如果输入正确,则针对数据库验证某些输入)。在这种情况下,我想验证用户的登录凭据。我的想法是:

AuthController.php:

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

EmailExists()是我的自定义规则。

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

但是我得到Too few arguments to function app\Validators\EmailExists::__construct(), 0 passed and exactly 1 expected的错误,由于AbstractRule没有注入PDO并且我的类对其进行了扩展,这在一定程度上是可以预料的。

因此,如何注入PDO接口,以便可以在自定义规则中使用它?

你们是否在使用另一种方法来验证这种数据?请注意,我正在编写API,因此数据库验证在某种程度上是必须的,并且在过去两天的谷歌搜索之后,我手头没有任何解决方案。

我也在使用PHP-DI创建PDO接口。这是我的dependencies.php文件:

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

和(部分)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);

任何帮助将不胜感激。

pdo respect-validation slim-4
1个回答
0
投票
class EmailAvailable extends AbstractRule {

    /**
     * @param $input
     *
     * @return bool
     */
    public function validate ($sInput) {
        return User::where('user_email', $sInput)->count() === 0;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.