OroPlatform: add custom field on core Entity

414 views Asked by At

I'm currently working on an OroPlatform project and I need to add a custom field on the BusinessUnit core entity.

I have read the Oro documentation section about the way to extend core entities : https://doc.oroinc.com/backend/entities/extend-entities/#id1

<?php
namespace MyBundle\Bundle\AppBundle\Migrations\Schema\v1_0;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;

class AddColumnsToBusinessUnit implements Migration
{
    public function up(Schema $schema, QueryBag $queries)
    {
        $table = $schema->getTable('oro_business_unit');
        $table->addColumn('siret', 'string', [
            'oro_options' => [
                'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
                'entity' => ['label' => 'siret'],
            ],
        ]);
    }
}

When I run the command symfony console oro:migration:load --force, it works and the migration is applied to my database.

Now, I want a required field. I have seen the instruction 'notnull' => true to setup a non nullable field on the database.

Everything works well, but my field hasn't any JavaScript validation on the organization/business_unit/create route. Any ideas ?

1

There are 1 answers

2
Andrey Yatsenko On BEST ANSWER

You can validate the new field by extending the validation metadata that is already defined for the core entity you are extending.

To do this, please follow the official Symfony documentation and use the YML format: https://symfony.com/doc/4.4/validation.html#constraint-configuration

The constraint that you can use for the field is "not blank."

Here is an example:

# src/<YourBundlePath>/Resources/config/validation.yml
Oro\Bundle\OrganizationBundle\Entity\BusinessUnit:
    properties:
        siret:
            - NotBlank: ~