简体   繁体   English

Symfony覆盖第三方捆绑验证

[英]Symfony override third-party bundle validation

if I am extending a third-party bundle's entity, how can I change/override validation for a specific property using annotations (leaving the current validation intact)? 如果要扩展第三方捆绑包的实体,如何使用注释更改/覆盖对特定属性的验证(保持当前验证不变)?

Thanks. 谢谢。

EDIT: in this case I am overriding the FOSUserBundle and the validation is set here vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation.xml : 编辑:在这种情况下,我覆盖了FOSUserBundle并在此处设置了验证: vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation.xml

<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
        http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="FOS\UserBundle\Model\User">

        <property name="username">
            <constraint name="NotBlank">
                <option name="message">fos_user.username.blank</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>Profile</value>
                </option>
            </constraint>  vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation.xml
            <constraint name="Length">
                <option name="min">2</option>
                <option name="minMessage">fos_user.username.short</option>
                <option name="max">255</option>
                <option name="maxMessage">fos_user.username.long</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>Profile</value>
                </option>
            </constraint>
        </property>

    <!-- other -->
    </class>
</constraint>

How can I add more constraints to this class? 如何向此类添加更多约束?

You need to create a new bundle and enable it in your application. 您需要创建一个新捆绑包,然后在您的应用程序中启用它。 Then, register the third-party Bundle as the "parent" of your bundle, here's an example from the docs : 然后,将第三方捆绑包注册为捆绑包的“父级”,这是docs中的示例:

// src/UserBundle/UserBundle.php
namespace UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class UserBundle extends Bundle
{
     public function getParent()
     {
         return 'FOSUserBundle';
     }
}

Then you can override any part of the Bundle just by creating a file with the same name, in your case it may be the model (if the validation is set there). 然后,您可以通过创建具有相同名称的文件来覆盖Bundle的任何部分,在您的情况下,它可能是模型(如果在此处设置了验证)。

Read more in the docs : https://symfony.com/doc/current/bundles/inheritance.html 在文档中阅读更多内容: https : //symfony.com/doc/current/bundles/inheritance.html

Edit 编辑

The docs does not mention overriding the validation : 该文档没有提及覆盖验证:

The Resources/config/validation.xml file contains definitions for custom validator rules for various classes. Resources / config / validation.xml文件包含各种类的定制验证器规则的定义。 The rules defined by FOSUserBundle are all in validation groups so you can choose not to use them. FOSUserBundle定义的规则全部在验证组中,因此您可以选择不使用它们。

But there's a discussion where it's mentioned that you can't override them, you choose to not use them, but can't add/remove/alter them. 但是在讨论中提到了您不能覆盖它们,您选择不使用它们,但是不能添加/删除/更改它们。

https://github.com/FriendsOfSymfony/FOSUserBundle/issues/986 https://github.com/FriendsOfSymfony/FOSUserBundle/issues/986

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM