David Barreto
David Barreto

Reputation: 9507

Running "bin/vendors install" with local modifications on the "vendors" folder in Symfony2

In Symfony2, when I run the command bin/vendors install I get the following message:

?? src/Symfony/Component/Validator/Constraints/Alphanumeric.php
?? src/Symfony/Component/Validator/Constraints/AlphanumericValidator.php
?? src/Symfony/Component/Validator/Constraints/GreaterThan.php
?? src/Symfony/Component/Validator/Constraints/GreaterThanValidator.php
"symfony" has local modifications. Please revert or commit/push them before running this command again.

The files listed, are custom constraint validators created by me following the cookbook entry here.

Is there a way to update the deps files ignoring the changes I made? My goal is to install a new bundle while keeping the constraint validator files created by me.

UPDATE: Peter solution was right, the only thing left is to "use" the correct namespace inside the entity like this:

(The code has words in Spanish and I will assume again that I'm in the DemoBundle just for consistency)

namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Acme\DemoBundle\Component\Validator\Constraints as CustomAssert;

/**
 * @ORM\Entity
 */
class Employee
{
    //...

    /**
     * @ORM\Column(type="string", length=20)
     * @Assert\NotBlank()
     * @CustomAssert\Alphanumeric()
     */
    protected $alfanum;

    //...
}

Upvotes: 1

Views: 657

Answers (3)

Mun Mun Das
Mun Mun Das

Reputation: 15002

You can put your custom validator in your own bundle and reference it in following way,

# in validation.yml

# define namespace identifier
namespaces:
  namespace_name: Path\To\Your\Validator\Namespace\

# then in your entity
FQCN\Of\Entity:
  constraints:
    - Path\To\Your\Validator\Namespace\Alphanumeric
    # or
    - "namespace_name:Alphanumeric": ~ 

Upvotes: 2

Peter Bailey
Peter Bailey

Reputation: 105888

No, don't do this. Custom validators (or custom anything) you write should be a part of your namespace, not Symfony's. And they certainly shouldn't be vendors directory at all (unless you write your own vendor). This is the whole point of namespacing and vendor management - to avoid collisions!

So, you need to move your custom validators to your application's source. Using the AcmeDemoBundle as an example...

Create directories for the following path

src/Acme/DemoBundle/Component/Validator/Constraints

And then move your custom validators into this folder. Then update the namespace for each validator class accordingly

namespace Acme\DemoBundle\Component\Validator\Constraints

Cheers

Upvotes: 2

JF Simon
JF Simon

Reputation: 1245

You should never change anything contained in the vendor directory. If you have new feature to add (and you always should have to), put these features in your application specific bundles (in the src directory).

Your namespaces/classes scheme should follow the framework convention. For instance your constraint validator should go in 'src/MyNamespace/MyBundle/Validator/Constraint' (and your namespace should be 'MyNamespace\MyBundle\Validator\Constraint').

Notice that the vendor directory should be ignored by your version manager.

Upvotes: 3

Related Questions