Fox
Fox

Reputation: 633

How can I store a datetime property in TYPO3?

I have created a simple form which handles an object person and this object owns the property birthDate which is of type datetime.

Now I want to create a person and store this property, but if I try to submit the form the following error occurs:

The date "%s" was not recognized (for format "%s")

I have read some threads and a lot of them suggest to use an initialize action to handle this problem, but this is not working for me.

Person:

class Person extends AbstractEntity
{
    ...

    /**
     * Stores the birth date of a person
     * 
     * @var \DateTime
     * @validate NotEmpty, DateTime
     *
     */
    protected $birthDate = null;

    ...
}

Fluid:

<f:form action="send" method="post" enctype="multipart/form-data" name="person" object="{person}">
...
<fieldset>
    <f:render partial="FormErrors" arguments="{field: 'person.birthDate'}" />
    <label class="required">Birthdate</label>
    <f:form.textfield
        type="date"
        placeholder="dd.mm.yyyy"
        property="birthDate"
    />
</fieldset>
...
</f:form>

My initialize action looks like the following:

public function initializeSendAction()
{
    if (isset($this->arguments['person'])) {
        $this->arguments['person']->getPropertyMappingConfiguration()->forProperty('birthDate')->setTypeConverterOption(
            'TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',
            \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
            'd.m.Y'
        );
    }
}

and the send action looks like:

public function sendAction(\Fox\Example\Domain\Model\Person $person)
{
    $this->personRepository->add($person);
    ...
}

So there is nothing special, but it seems that my custom datetime format "d.m.Y" is not working and the property is of type string, any suggestions?

Upvotes: 1

Views: 3720

Answers (1)

Ghanshyam Gohel
Ghanshyam Gohel

Reputation: 1274

One of my working example, may helps you.

TCA:

'geburtsdatum' => array(
    'exclude' => 1,
    'label' => 'Geburtsdatum',
    'config' => array(
        'dbType' => 'date',
        'type' => 'input',
        'size' => 7,
        'eval' => 'date',
        'checkbox' => 0,
        'default' => '0000-00-00'
    ),
),

SQL:

geburtsdatum date DEFAULT '0000-00-00',

Model:

class Inquiry extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    ...

    /**
     * geburtsdatum
     *
     * @var string
     */
    protected $geburtsdatum = NULL;

    ...
    /**
     * Returns the geburtsdatum
     *
     * @return string $geburtsdatum
     */
    public function getGeburtsdatum()
    {
        return $this->geburtsdatum;
    }

    /**
     * Sets the geburtsdatum
     *
     * @param string $geburtsdatum
     * @return void
     */
    public function setGeburtsdatum($geburtsdatum)
    {
        $this->geburtsdatum = $geburtsdatum;
    }
}

Fluid:

<f:form.textfield id="datepicker" placeholder="dd.mm.yyyy" property="geburtsdatum" />

Controller:

protected function initializeCreateAction(){
    $propertyMappingConfiguration = $this->arguments['newInquiry']->getPropertyMappingConfiguration();
    $propertyMappingConfiguration->allowAllProperties();
    $propertyMappingConfiguration->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, TRUE);
}
public function createAction(\TYPO3Liebhaber\extkey\Domain\Model\Inquiry $newInquiry){
    $arguments = $this->request->getArguments();
    if($arguments['newInquiry']['geburtsdatum']){
        $geburtsdatum = date('Y-m-d',strtotime($arguments['newInquiry']['geburtsdatum']));
        $newInquiry->setGeburtsdatum($geburtsdatum);
    }
    $this->inquiryRepository->add($newInquiry);
}

Upvotes: 1

Related Questions