Reputation: 967
I have got a form, based on Extbase an Fluid in TYPO3 7.6. Every time a validation error in any field occurs, the form gets displayed again showing the validation errors, as desired.
But every time the form gets displayed again and a value has been entered in the date field, the error message htmlspecialchars() expects parameter 1 to be string, object given
appears.
I would like to get rid of this error message.
The controller has a date property in it:
<?php
namespace Vendor\Extension\Domain\Model;
class Person extends BaseDto
{
/**
* @var \DateTime $privatePersonBirthdate
*/
protected $privatePersonBirthdate;
/**
* @param \DateTime $privatePersonBirthdate
*/
public function setPrivatePersonBirthdate($privatePersonBirthdate)
{
$this->privatePersonBirthdate = $privatePersonBirthdate;
}
/**
* @return \DateTime
*/
public function getPrivatePersonBirthdate()
{
return $this->privatePersonBirthdate;
}
}
Template:
<f:form.textfield property="privatePersonBirthdate" />
Property configuration in controller:
$conf->forProperty('privatePersonBirthdate')->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'd.m.Y');
I already found some similar reports online, like https://forge.typo3.org/issues/4268.
The workaround using the value attribute would work, but also destroys the value the user entered in the form on validation errors:
<f:form.textfield property="privatePersonBirthdate" value="{f:format.date(date: person.privatePersonBirthdate, format: 'd.m.Y')}" />
I seems like something is missing. I do not get it. Do you have an idea how to solve this issue?
Upvotes: 0
Views: 587
Reputation: 967
I solved the issue creating a view helper which converts the property of type DateTime
before trying to render it:
<?php
namespace Vendor\Extension\ViewHelpers;
use TYPO3\CMS\Fluid\ViewHelpers\Form\TextfieldViewHelper;
/**
* This view helper solves the issue described https://stackoverflow.com/questions/45792891/typo3-7-6-extbase-fluid-form-htmlspecialchars-expects-parameter-1-to-be-str
*
* Class TextfieldForDatesViewHelper
* @package Educo\Eddaylight\ViewHelpers
*/
class TextfieldForDatesViewHelper extends TextfieldViewHelper
{
/**
* Initialize the arguments.
*
* @return void
* @api
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerTagAttribute(self::DATEFORMAT, 'string', 'Required format for date field');
}
/**
* Converts an arbitrary value to a plain value
*
* @param mixed $value The value to convert
* @return mixed
*/
protected function convertToPlainValue($value)
{
if ($value instanceof \DateTime) {
return $value->format($this->arguments[self::DATEFORMAT]);
}
return parent::convertToPlainValue($value);
}
const DATEFORMAT = 'dateFormat';
}
Upvotes: 0