Sonataadminbundle: [Form] 'required' option is always true if the type of a field is passed.

Created on 22 Sep 2016  路  7Comments  路  Source: sonata-project/SonataAdminBundle

Environment

Symfony 2.8.8 & Sonata Admin 3.4.0 with Doctrine ORM

Sonata packages

$ composer show sonata-project/*
sonata-project/admin-bundle              3.4.0  The missing Symfony Admin Generator
sonata-project/block-bundle              3.1.1  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.0.3  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.2    Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.0.5  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.1.10 Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.5.0  Lightweight Exporter library
sonata-project/google-authenticator      1.0.2  Library to integrate Google Authenticator into a PHP project
sonata-project/intl-bundle               2.2.4  Symfony SonataIntlBundle
sonata-project/user-bundle               3.0.1  Symfony SonataUserBundle

Symfony packages

$ composer show symfony/*
symfony/assetic-bundle     dev-master aa5b4f8 Integrates Assetic into Symfony2
symfony/monolog-bundle     2.11.1             Symfony MonologBundle
symfony/phpunit-bridge     v2.8.8             Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.2.0             Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu  v1.2.0             Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.2.0             Symfony polyfill for the Mbstring extension
symfony/polyfill-php54     v1.2.0             Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55     v1.2.0             Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56     v1.2.0             Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.2.0             Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.2.0             Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.0             Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.3.11            Symfony SwiftmailerBundle
symfony/symfony            v2.8.8             The Symfony PHP framework

PHP version

$ php -v
PHP 5.6.26-1+deb.sury.org~xenial+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Subject

When adding a field on the FormMapper, even if the field has nullable=true and no assert, it will be show as required field if the type (second argument) is passed.

Tried with date and float field, using Symfony's MoneyType and sonata_type_date_picker.

The fields are:

/**
     * Purchase Price
     * @ORM\Column(type="float", nullable=true)
     */
    private $purchasePrice;
/**
     * Warraty Start Date
     * @ORM\Column(type="date", nullable=true)
     */
    private $warrantyStartDate;

And the admin code is:

$formMapper
    ->add('purchasePrice', MoneyType::class, array('currency' => 'BRL'))
    ->add('warrantyStartDate', 'sonata_type_date_picker', array('format' => 'dd/MM/yyyy'))
;

Both fields will be rendered as required fields, but if I remove the second argument, the will be rendered as not required fields.

Most helpful comment

@ejkun egree with you, it's not conveniently!

All 7 comments

You should pass 'required' => false in third argumet

$formMapper
    ->add('purchasePrice', MoneyType::class, array('currency' => 'BRL', 'required' => false))
    ->add('warrantyStartDate', 'sonata_type_date_picker', array('format' => 'dd/MM/yyyy', 'required' => false))
;

@ruslan-naryzhniy, that's what I'm doing to fix it, but the option should be guessed automatically, don't you think?

@ejkun egree with you, it's not conveniently!

3554 the same issue

I think it is different. That one is a problem related to one_to_one relations and sonata_type_admin.
I'm trying to figure out which files are related to this problem, when I do, I will start creating a PR for it.
Any help is welcome.

@ejkun Symfony Form component set fields required by default. I think we should customize Sonata Form. What about to use doctrine annotation render? Now I am working on it.

The PR was discarded. So the issue does not make a lot of sense. Closing for now.

If you think this is a problem on sonata side, please ping me to reopen

Was this page helpful?
0 / 5 - 0 ratings