Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Date on Form rendering (LogicException: Cannot use the "format" option of DateType when the html5 option is enabled) #400

Open
etiennerunge opened this issue Nov 19, 2024 · 1 comment

Comments

@etiennerunge
Copy link

Q A
Bug? yes
New Feature? no
Bundle Version 2.5.4
Sulu Version 2.6.5

Actual Behavior

Rendering Date (Symfony\Component\Form\Extension\Core\Type\DateType) doesn't work because of an error.

Expected Behavior

DateType should be rendered with default format without error

Steps to Reproduce

Add Date form field in form. This throws an error on page rendering: Cannot use the "format" option of "Symfony\Component\Form\Extension\Core\Type\DateType" when the "html5" option is enabled.

Possible Solutions

DateType field have by default html5 option value set to true.

But this is incompatible with the value of the format option set to IntlDateFormatter::LONG

// Sulu\Bundle\FormBundle\Dynamic\Types\DateType
        $type = TypeDateType::class;
        $translation = $field->getTranslation($locale);
        if ($translation && $translation->getOption('birthday')) {
            $type = BirthdayType::class;
        }
        $options['format'] = \IntlDateFormatter::LONG;
        $options['input'] = 'string';
        $builder->add($field->getKey(), $type, $options);

Could dateformat be configured in sulu form yaml config file, and html5 set to false?

Maybe this is relative to this issue : #49

@etiennerunge
Copy link
Author

I found a little workaround.
Simply decorate Sulu\Bundle\FormBundle\Dynamic\Types\DateType in service.yaml
To use custom DateType class instead of the sulu one.

# services.yaml
    App\Form\Types\SuluFormDateTypeDecorator:
        decorates: sulu_form.dynamic.type_date
        tags:
            - { name: 'sulu_form.dynamic.type', alias: 'date' }
// src/Form/Types/SuluFormDateTypeDecorator.php
<?php

namespace App\Form\Types;

use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Dynamic\Types\DateType as SuluDateType;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\DateType as TypeDateType;
use Symfony\Component\Form\FormBuilderInterface;

class SuluFormDateTypeDecorator implements FormFieldTypeInterface
{
    public function __construct(private SuluDateType $suluDateType)
    {
    }

    public function getConfiguration(): FormFieldTypeConfiguration
    {
        return new FormFieldTypeConfiguration(
            'sulu_form.type.date',
            __DIR__ . '/../../../vendor/sulu/form-bundle/Resources/config/form-fields/field_date.xml',
            'basic',
        );
    }

    public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
    {
        $type = TypeDateType::class;
        $translation = $field->getTranslation($locale);
        if ($translation && $translation->getOption('birthday')) {
            $type = BirthdayType::class;
        }
        $options['format'] = 'dd/MM/yyyy';
        $options['input'] = 'string';
        $options['html5'] = false;
        $builder->add($field->getKey(), $type, $options);
    }

    public function getDefaultValue(FormField $field, string $locale)
    {
        $value = $field->getTranslation($locale)->getDefaultValue();

        return $value;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant