【问题标题】:Validation within a Symfony BundleSymfony Bundle 中的验证
【发布时间】:2021-01-15 07:26:49
【问题描述】:

您如何在创建的包中实现 Symfony 验证器?

我有一个 Extension 类、一个 CompilerPass 和一个“services.xml”文件。

验证器应该注入中间件,我在我的扩展中尝试使用:
$container->registerForAutoConfiguration(ValidatorInterface::class)->addTag(...);
但如果我尝试在 CompilerPass 中查找标签并转储密钥,则它声称我请求了未定义的服务。

【问题讨论】:

  • 你不需要对你的扩展类做任何事情。您可以将 ValidatorInterface 注入到您需要的服务中。只需将其放入带有类型提示的构造函数中即可。 ValidatorInterface $validator
  • Afaiui,默认情况下捆绑包不会自动配置,因此至少您需要在services.xml 中添加一些接线;我现在似乎必须通过将 PHP 配置添加到扩展的 prepend() 方法中的 framework 键来工作;我认为否则应用程序需要实现这一点? (而且我不确定以这种方式在实际捆绑包中使用验证器是否可行......)。如果我错了,请告诉我(请参阅下面的答案)。

标签: php symfony validation bundle


【解决方案1】:

您需要有一些可以验证的东西,例如信使中间件:

<?php declare(strict_types=1);

namespace User\MyBundle\Middleware;

use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class ValidationMiddleware implements MiddlewareInterface
{
    private ValidatorInterface $validator;

    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        $errors = $this->validator->validate($envelope->getMessage());

        if (!empty($errors)) {
            // exception
        }

        return $stack->next()->handle($envelope, $stack);
    }
}

并使用validator作为服务注入配置的id:

<!-- file: src/Resources/config/services.xml -->

    <service id="my.middleware.validator" class="User\MyBundle\Middleware\ValidatorMiddleware">
        <tag name="messenger.middleware" />
        <argument type="service" id="validator" />
    </service>

然后使用 PrependExtensionInterface 在扩展类中配置框架,就像在普通应用程序中一样:

<?php declare(strict_types=1);

namespace User\MyBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class UserMyExtension extends Extension implements PrependExtensionInterface
{
    public function load(array $configs, ContainerBuilder $container)
    {
        // configure dependencies
    }

    public function prepend(ContainerBuilder $container)
    {
        $container->prependExtensionConfig('framework', [
            'validation' => [
                'enabled' => true,
                'enable_annotations' => false,
                'mapping' => [
                    'paths' => [
                        __DIR__ . "/../Resources/config/validation/validator.xml",
                    ],
                ],
            ],
        ]);
    }
}

【讨论】:

    【解决方案2】:

    如果我们采用更简单的方法会怎样。您可以通过在您的自述文件中添加一个简单的安装说明告诉您的捆绑包的用户安装您的验证规则,告诉用户编辑config/packages/validator.yaml

    auto_mapping:
            YourVendor\YourBundle\Entity\: []
        mapping:
            paths:
                - '%kernel.project_dir%/lib/src/YourBundle/Resources/config/validator.yaml'
    

    上面显示的示例在项目目录中有目录。为方便起见,可以对其进行编辑。在您的 validator.yaml 文件中,您可以放置​​您的验证规则

    YourVendor\YourBundle\Entity\Institution:
    properties:
        email:
            - Email: { message: Please Enter a Valid Email Address }
    constraints:
        - YourVendor\YourBundle\Validators\Institution\ClosingTime: ~
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 2012-10-30
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      相关资源
      最近更新 更多