0

I have two issues while using the Zend-Form date element.

First: field binding The edit action within my controller doesn't fillin an existing date. For example birthday. The field is just empty. (with an element type text, there is no problem).

Here how I instanciated the field:

        $this->add([
        'name' => 'geburtstag',
        'type' => 'date',
        'options' => [
            'label' => 'Geburtstag:',
            'format' => 'dd/mm/yyyy',
        ],
    ]);

And here my controller action.

    public function addAction()
{
    $form = new AnsprechpartnerForm(NULL, $this->db);
    $form->get('submit')->setValue('save');
    $request = $this->getRequest();
    if (! $request->isPost()) {
        return ['form' => $form];
    }
    $ansprechpartner = new Ansprechpartner();
    $form->setInputFilter($ansprechpartner->getInputFilter());
    $form->setData($request->getPost());
    if (! $form->isValid()) {
        return ['form' => $form];
    }
    $ansprechpartner->exchangeArray($form->getData());
    $this->ansprechpartnerTable->saveAnsprechpartner($ansprechpartner);
    return $this->redirect()->toRoute('ansprechpartner');
}

No inputFilter at the moment, I tried with and without.

Second: validation

I have trouble filling in dates. While I don't use any filters for this field, I would expect, I could fill any date in.

enter image description here

Interesting I get the message double.

pia-sophie
  • 505
  • 4
  • 21

1 Answers1

-1

I solved it.

The date element expects the format y-m-d. Now I gave it directly to the field after binding the form. The format in the field is now also korrekt.

$form->bind($notizen);
$form->get('submit')->setAttribute('value', 'edit');
$filter = new \Zend\Filter\DateTimeFormatter();
$filter->setFormat('Y-m-d');
$dat = $filter->filter($notizen->datum);
$form->get('datum')->setValue($dat);

Could be more convenient I guess.

pia-sophie
  • 505
  • 4
  • 21
  • It would be better to do this configuration inside the form creation and not in the controller where you're using it.. ;) – Ermenegildo Jun 13 '19 at 08:06