Setting default value of Date FormElement

I added a Date FormElement to my form using the following code:

            $this->add([
                'type' => Date::class,
                'name' => 'examinationDate',
                'options' => [
                    'label' => 'Prüfungsdatum',
                ],
                'attributes' => [
                    'value' => \date('Y-m-d'),
                ],
            ]);

However, the default value is not set on the web page. Am I missing something?

Works as expected:

$form = new Laminas\Form\Form();
$form->add(
    [
        'type'       => Laminas\Form\Element\Date::class,
        'name'       => 'examinationDate',
        'options'    => [
            'label' => 'Prüfungsdatum',
        ],
        'attributes' => [
            'value' => \date('Y-m-d'),
        ],
    ]
);

$renderer = new Laminas\View\Renderer\PhpRenderer();
$renderer->getHelperPluginManager()->configure(
    (new Laminas\Form\ConfigProvider())->getViewHelperConfig()
);

echo $renderer->form($form);

Result:

<form action="" method="POST">
    <label>
        <span>Prüfungsdatum</span>
        <input type="date" name="examinationDate" value="2024-01-17">
    </label>
</form>

See:

Have you overwritten the element or the view helper somehow? Or do you set data to form with empty values:

$form->setData(['examinationDate' => '']);

Results in:

<input type="date" name="examinationDate" value="">

Found the error: The entity I tested on was beeing edited instead of created and had null as value for this field.

Thank you for the quick reply :slight_smile: