sonata-project / SonataUserBundle

Symfony SonataUserBundle
https://docs.sonata-project.org/projects/SonataUserBundle
MIT License
342 stars 487 forks source link

Custom validation not working at all #1077

Closed 7system7 closed 4 years ago

7system7 commented 5 years ago

I would like to create a custom validation method in my UserAdmin. It checks a field (property) that depends on another field. The UserAdmin was created w/ "easy-extends" command:

$ bin/console sonata:easy-extends:generate SonataUserBundle --dest=src --namespace_prefix=App

Every validation method in the administration area are working properly, except the generated ones, include the SonataUserBundle It is completely ignore the validation method in Admin class... I tried everything; Documentation, I was read the code deeeeeeeeply, Google, DuckDuckGo, Github issues, stackoverflow, cache-invalidation, avada-kedavra.... And sadly, (I think) the documentation is very poor.

I think it is a bug but if I miss something thank you for your co-operation in advance. (And sry for my English.)

Environment

Linux AMBER 4.18.0-13-generic #14-Ubuntu SMP Wed Dec 5 09:04:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.45.1 3.45.1 The missing Symfony Admin Generator
sonata-project/block-bundle              3.14.0 3.14.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.15.0 3.15.1 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.4.0  2.4.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.1.5  1.1.5  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.8.0  3.8.1  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.0  2.0.1  Lightweight Exporter library
sonata-project/media-bundle              3.18.1 3.18.1 Symfony SonataMediaBundle
sonata-project/translation-bundle        2.4.0  2.4.0  SonataTranslationBundle
sonata-project/user-bundle               4.2.3  4.3.0  Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                v4.2.2  v4.2.2  Symfony Asset Component
symfony/cache                v4.2.2  v4.2.2  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config               v4.2.2  v4.2.2  Symfony Config Component
symfony/console              v4.2.2  v4.2.2  Symfony Console Component
symfony/contracts            v1.0.2  v1.0.2  A set of abstractions extracted out of the Symfony components
symfony/debug                v4.2.2  v4.2.2  Symfony Debug Component
symfony/debug-bundle         v4.2.2  v4.2.2  Symfony DebugBundle
symfony/debug-pack           v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection v4.2.2  v4.2.2  Symfony DependencyInjection Component
symfony/doctrine-bridge      v4.2.2  v4.2.2  Symfony Doctrine Bridge
symfony/dom-crawler          v4.2.2  v4.2.2  Symfony DomCrawler Component
symfony/dotenv               v4.2.2  v4.2.2  Registers environment variables from a .env file
symfony/event-dispatcher     v4.2.2  v4.2.2  Symfony EventDispatcher Component
symfony/expression-language  v4.2.2  v4.2.2  Symfony ExpressionLanguage Component
symfony/filesystem           v4.2.2  v4.2.2  Symfony Filesystem Component
symfony/finder               v4.2.2  v4.2.2  Symfony Finder Component
symfony/flex                 v1.1.8  v1.1.8  Composer plugin for Symfony
symfony/form                 v4.2.2  v4.2.2  Symfony Form Component
symfony/framework-bundle     v4.2.2  v4.2.2  Symfony FrameworkBundle
symfony/http-foundation      v4.2.2  v4.2.2  Symfony HttpFoundation Component
symfony/http-kernel          v4.2.2  v4.2.2  Symfony HttpKernel Component
symfony/inflector            v4.2.2  v4.2.2  Symfony Inflector Component
symfony/intl                 v4.2.2  v4.2.2  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle         v1.11.3 v1.11.3 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/monolog-bridge       v4.2.2  v4.2.2  Symfony Monolog Bridge
symfony/monolog-bundle       v3.3.1  v3.3.1  Symfony MonologBundle
symfony/options-resolver     v4.2.2  v4.2.2  Symfony OptionsResolver Component
symfony/orm-pack             v1.0.6  v1.0.6  A pack for the Doctrine ORM
symfony/polyfill-ctype       v1.10.0 v1.10.0 Symfony polyfill for ctype functions
symfony/polyfill-intl-icu    v1.10.0 v1.10.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring    v1.10.0 v1.10.0 Symfony polyfill for the Mbstring extension
symfony/profiler-pack        v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access      v4.2.2  v4.2.2  Symfony PropertyAccess Component
symfony/routing              v4.2.2  v4.2.2  Symfony Routing Component
symfony/security-acl         v3.0.1  v3.0.1  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle      v4.2.2  v4.2.2  Symfony SecurityBundle
symfony/security-core        v4.2.2  v4.2.2  Symfony Security Component - Core Library
symfony/security-csrf        v4.2.2  v4.2.2  Symfony Security Component - CSRF Library
symfony/security-guard       v4.2.2  v4.2.2  Symfony Security Component - Guard
symfony/security-http        v4.2.2  v4.2.2  Symfony Security Component - HTTP Integration
symfony/stopwatch            v4.2.2  v4.2.2  Symfony Stopwatch Component
symfony/swiftmailer-bundle   v3.2.5  v3.2.5  Symfony SwiftmailerBundle
symfony/templating           v4.2.2  v4.2.2  Symfony Templating Component
symfony/translation          v4.2.2  v4.2.2  Symfony Translation Component
symfony/twig-bridge          v4.2.2  v4.2.2  Symfony Twig Bridge
symfony/twig-bundle          v4.2.2  v4.2.2  Symfony TwigBundle
symfony/validator            v4.2.2  v4.2.2  Symfony Validator Component
symfony/var-dumper           v4.2.2  v4.2.2  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter         v4.2.2  v4.2.2  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle  v4.2.2  v4.2.2  Symfony WebProfilerBundle
symfony/webpack-encore-pack  v1.0.3  v1.0.3  A pack for Symfony Encore
symfony/yaml                 v4.2.2  v4.2.2  Symfony Yaml Component

PHP version

$ php -v
PHP 7.3.1-1+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Jan 13 2019 10:20:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.1-1+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Subject

Below, I will show my all methods I tried.

Steps to reproduce

So, I override the validate method in UserAdmin \App\Application\Sonata\UserBundle\Admin\Model\UserAdmin::validate

    /**
     * {@inheritdoc}
     */
    public function validate(ErrorElement $errorElement, $object)
    {
        die();

        parent::validate($errorElement, $object); // TODO: Change the autogenerated stub
    }

Method 1

It works alone w/ all Admin that I created by hand. (for example: DealAdmin) But I was read some similar issues. Everybody says I must create a validation group. So I created it in \App\Application\Sonata\UserBundle\Admin\Model\UserAdmin::getFormBuilder

    /**
     * {@inheritdoc}
     */
    public function getFormBuilder()
    {
        $this->formOptions = array('validation_groups' => 'PartnerValidation');
        return parent::getFormBuilder(); // TODO: Change the autogenerated stub
    }

AND I was override the $formOptions property

protected $formOptions = [
    'validation_groups' => ['PartnerValidation']
];

I was override the User:username property and I was add an assertion. \App\Application\Sonata\UserBundle\Entity\User::$username

    /**
     * @var string
     *
     * @Assert\Length(
     *      min = 2,
     *      max = 3,
     *      groups={"PartnerValidation"},
     *      minMessage = "Your first name must be at least {{ limit }} characters long",
     *      maxMessage = "Your first name cannot be longer than {{ limit }} characters"
     * )
     */
    protected $username;

Method 2

I created a service: config/services.yaml

sonata.partner.validation:
        public: true
        class: App\Application\Sonata\UserBundle\Entity\Block\PartnerBlockService

I was add the InlineConstraint class constraint to my bundle’s validation configuration: src/Application/Sonata/UserBundle/Resources/config/validation.yaml I tried w/ all of these src/Application/Sonata/UserBundle/Resources/config/validation.yml src/Application/Sonata/UserBundle/Resources/config/validation.xml (and the xml code - naturally - does not look like that below)

App\Application\Sonata\UserBundle\Entity\Block:
    constraints:
        -   Sonata\Form\Validator\Constraints\InlineConstraint:
                service: sonata.partner.validation
                method: validateBlock

I was create the service where the validation method defined: \App\Application\Sonata\UserBundle\Entity\Block\PartnerBlockService

<?php

namespace App\Application\Sonata\UserBundle\Entity\Block;

use Sonata\UserBundle\Model\UserInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\Form\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;

/**
 * Class PartnerBlockService
 *
 * @package App\Application\Sonata\UserBundle\Entity\Block
 * @author  ...
 */
class PartnerBlockService extends AbstractBlockService
{
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
        die();
        ...
    }
}

Expected results

It runs into validate or validateBlock method and dies.

Actual results

Absolutely nothing. There is no any error message or exception.

7system7 commented 5 years ago

Method 3 - "native", Symfony way

I created the global validation.yaml config/validator/validation.yaml

App\Application\Sonata\UserBundle\Entity\User:
    constraints:
        - App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmail: ~
    properties:
        username:
            -   NotBlank: { groups: [PartnerValidation] }
            -   Length: { min: 5, max: 10, groups: [PartnerValidation] }

I created the (class) Constraint \App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmail

namespace App\Application\Sonata\UserBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class ChecksPartnerEmail extends Constraint
{
    public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return get_class($this).'Validator';
    }
}

And I created the Validator class \App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmailValidator

namespace App\Application\Sonata\UserBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ChecksPartnerEmailValidator extends ConstraintValidator
{
    public function validate($protocol, Constraint $constraint)
    {
        dump(
            'bazmeg'
        );
        die();

        ...
    }
}

And "just in case" I added the Constraint to the User entity as an Annotation also \App\Application\Sonata\UserBundle\Entity\User

use App\Application\Sonata\UserBundle\Validator\Constraints as BackendAssert;

...

/**
 * This file has been generated by the SonataEasyExtendsBundle.
 *
 * @link https://sonata-project.org/easy-extends
 *
 * References:
 * @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 *
 * @BackendAssert\ChecksPartnerEmail
 */
class User extends BaseUser

The Symfony's Basic Constraints are working. The custom Constraint (ChecksPartnerEmail) is not.

7system7 commented 5 years ago

Nevemind. I solved it w/ Expression.

sebvie commented 4 years ago

same issue here

core23 commented 4 years ago

same issue here

This is not how Open Source work... Maybe you could investigate a little bit and provide a PR with a fix.

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

smilearric commented 4 years ago

Nevemind. I solved it w/ Expression.

Hello. facing the same issue, we would have loved to know how...

Please @7system7 could you explain what you did, roughly at least?

Regards

7system7 commented 4 years ago

@smilearric

This is a symfony way to solve the problem. You have 2 good options. Constraints or expressions. I am not sure, you have to do every step, I just copy-paste my solution to here.

config/packages/framework.yaml

framework:
    validation: { enabled: true }

config/validator/validation.yaml

App\Entity\Deal:
    constraints:
        - App\Validator\Constraints\DealStageConstraint: ~

App\Entity\DealActivity:
    properties:
        activityType:
            -   Expression: { groups: [DealActivityValidation], expression: "this.activityTypeValidation()", message: <your-message-for-validation-error>}

w/ expressions

src/Admin/DealActivityAdmin.php

class DealActivityAdmin
{

...
    /**
     * {@inheritdoc}
     */
    // ----> IMPORTANT! Overridden method from Sonata\AdminBundle\Admin\AbstractAdmin
    public function getFormBuilder()
    {
        $this->formOptions['data_class'] = $this->getClass();

        $options = $this->formOptions;
        // ----> IMPORTANT! That is the point
        $options['validation_groups'] = array('Default', 'DealActivityValidation');
        $options['attr'] = array('novalidate' => 'novalidate');

        $formBuilder = $this->getFormContractor()->getFormBuilder($this->getUniqid(), $options);

        $this->defineFormBuilder($formBuilder);

        return $formBuilder;
    }

...

}

src/Entity/DealActivity.php

class DealActivity
{

...

    /**
     * Validation for activityType
     *
     * @return bool
     */
    public function activityTypeValidation(): bool
    {
        return <your-validation-conditions>;
    }

...

{

w/ constraints

src/Validator/Constraints/DealStageConstraint.php

class DealStageConstraint extends Constraint
{
...
    public string $message = '<your-validation-message>';

    /**
     * {@inheritDoc}
     */
    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }
...
}

src/Validator/DealStageConstraintValidator.php

final class DealStageConstraintValidator extends ConstraintValidator
{
...
    public function validate($protocol, Constraint $constraint): void
    {
    ...
           // example for a validation message to Deal/dealStage property (input)
            $this->context
                ->buildViolation($constraint->message)
                ->atPath('dealStage')
                ->addViolation();
    ...
    }
...
}
github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

karenzio commented 2 years ago

Just met same problem and get solved after a try.

  1. Change your custom UserAdmin to directly extends Sonata\AdminBundle\Admin\AbstractAdmin instead
  2. Copy and fit in all necessary things to your custom UserAdmin from Sonata\UserBundle\Admin\Model\UserAdmin

Then the validate function will works properly.

Hope this would help someone in the future :)

playaer commented 1 year ago

Any way: I add groups={"Profile"} into my annotation for my entity and validate function will works.

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
.....
@UniqueEntity(fields={"terminalPassword"}, ignoreNull=true, groups={"Profile"})