sonata-project / SonataCoreBundle

[deprecated] SonataCoreBundle
MIT License
318 stars 138 forks source link

Validator for DateRange missing or not working #705

Closed Hanmac closed 3 years ago

Hanmac commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'

sonata-project/admin-bundle              3.49.1             3.49.1             The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0             3.15.0             Symfony SonataBlockBundle
sonata-project/cache                     2.0.1              2.0.1              Cache library
sonata-project/core-bundle               3.17.0             3.17.0             Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0              3.0.0              Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0              1.3.0              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0              3.9.0              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.1              2.0.1              Lightweight Exporter library
sonata-project/user-bundle               dev-master 2b1747e dev-master 2b1747e Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/acl-bundle            dev-master 5b32179 dev-master 5b32179 Symfony AclBundle
symfony/assetic-bundle        v2.8.2             v2.8.2             Integrates Assetic into Symfony2
symfony/http-client           v4.3.1             v4.3.1             Symfony HttpClient component
symfony/http-client-contracts v1.1.3             v1.1.3             Generic abstractions related to HTTP clients
symfony/mime                  v4.3.1             v4.3.1             A library to manipulate MIME messages
symfony/monolog-bundle        v3.3.1             v3.3.1             Symfony MonologBundle
symfony/phpunit-bridge        v2.8.50            v4.3.1             Symfony PHPUnit Bridge
symfony/polyfill-apcu         v1.11.0            v1.11.0            Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-ctype        v1.11.0            v1.11.0            Symfony polyfill for ctype functions
symfony/polyfill-intl-icu     v1.11.0            v1.11.0            Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn     v1.11.0            v1.11.0            Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring     v1.11.0            v1.11.0            Symfony polyfill for the Mbstring extension
symfony/polyfill-php56        v1.11.0            v1.11.0            Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70        v1.11.0            v1.11.0            Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-php72        v1.11.0            v1.11.0            Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73        v1.11.0            v1.11.0            Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-util         v1.11.0            v1.11.0            Symfony utilities for portability of PHP codes
symfony/security-acl          v3.0.2             v3.0.2             Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle    v2.6.7             v3.2.8             Symfony SwiftmailerBundle
symfony/symfony               v3.4.28            v4.3.1             The Symfony PHP framework

PHP version

$ php -v
# Put the result here.

PHP 7.3.5 (cli) (built: May 1 2019 13:17:17) ( ZTS MSVC15 (Visual C++ 2017) x64 ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans

Subject

I have an Entity with both from and to Attributes, and i wanted to use the DateRangePicker for this

Entity code:


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="fromDate", type="datetime", nullable=true)
     */
    private $from;

    /**
     * @var \DateTime
     *
     * @Assert\GreaterThan(propertyPath="from")
     * @ORM\Column(name="toDate", type="datetime", nullable=true)
     */
    private $to;

    public function getDateRange() {
        return [
            "start" => $this->getFrom(),
            "end" => $this->getTo()
        ];
    }

    public function setDateRange($range) {
        if (isset($range['start'])) {
            $this->setFrom($range['start']);
        }
        if (isset($range['end'])) {
            $this->setTo($range['end']);
        }
        return $this;
    }

Form code:

->add('date_range', DateRangePickerType::class, array(
            'required' => true,
            'label' => false,
            'error_mapping' => [
                'from' => 'date_range',
                'to' => 'date_range'
            ],
            'field_options' => ['required' => true],
            'field_options_end' => [
                'dp_min_date' => $planung->getFrom(),
            ]
        ))

The thing now with the Date Range Picker is this, that if you only change the Start Date, the End Date might be before the Start Date and so invalid. (there is maybe js validaton or html5 validation missing)

The Problem is now that while the Symfony Validation does work and does says the Form is invalid, the error isn't shown on the right Form Field.

I don't know yet what the right thing would be.

Expected results

Either before Form Submit, the JS/HTML Validator should show the Error And/Or the Symfony validator should be shown in the Form that is for the other property

Actual results

Form without any Error shown

jordisala1991 commented 3 years ago

Please open the issue on form-extension package of sonata if the problem still persist after upgrade