简体   繁体   中英

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 :

<?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 :

// 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).

Read more in the docs : 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. The rules defined by FOSUserBundle are all in validation groups so you can choose not to use them.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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