TYPO3 Extbase: persisting domain model with datetime property

我怕爱的太早我们不能终老 提交于 2020-01-02 21:48:06

问题


I am really stuck here (maybe the sickness is not helping...).

Setting: - typo3 6.2 with according extbase and fluid - checked about 20 blog entries, the official docu, and a few code snippets I found - Senior on developing TYPO3, but relatively new to extbase/fluid

Goal: As many of you, I try to realize a form in the frontend to create a new object. This object contains some datetime properties.

Symptoms: - I can create an object in the backend and correctly output it with fluid creating lists and having some fun with it, no problem so far. - When filling out the form I get the all known FlashErrorMessage "An error occurred while trying to call" - After styling the form fields that give an error (used the f3-form-error css-class) I could determine the input fields of the datetime properties as the bad guys.

tca excerpt:

'startdate' => array(
    'label' => 'LLL:EXT:ngibmembers/Resources/Private/Language/locallang_db.xlf:paymentperiod.startdate',
    'config' => array(
        'type' => 'input',
        'size' => 30,
        'eval' => 'date',
    ),
),
'stopdate' => array(
    'label' => 'LLL:EXT:ngibmembers/Resources/Private/Language/locallang_db.xlf:paymentperiod.stopdate',
    'config' => array(
        'type' => 'input',
        'size' => 30,
        'eval' => 'date',
    ),
),

then here the domain model part:

/**
 * startdate
 *
 * @var \DateTime
 * @validate DateTime
 */
protected $startdate;

/**
 * stopdate
 *
 * @var \DateTime
 * @validate DateTime
 */
protected $stopdate;

/**
 * Get startdate
 *
 * @return \DateTime
 */
 public function getStartdate(){
     return $this->startdate;
 }

 /**
  * Set startdate
  *
  * @param \DateTime $startdate
  * @return Paymentperiod
  */
  public function setStartdate($startdate){
      $this->startdate = $startdate;
      return $this;
  }

/**
 * Get stopdate
 *
 * @return \DateTime
 */
 public function getStopdate(){
     return $this->stopdate;
 }

/**
 * Set stopdate
 *
 * @param \DateTime $stopdate
 * @return Paymentperiod
 */
 public function setStopdate($stopdate){
     $this->stopdate = $stopdate;
     return $this;
 }

the Actions

/**
 * convert date properties
 *
 * @return void
 */
public function initializeAction(){
$dateFormat = 'dd.mm.yyyy';
if (isset($this->arguments['paymentperiod'])) {
    $this->arguments['paymentperiod']
        ->getPropertyMappingConfiguration()
        ->forProperty('startdate')
        ->setTypeConverterOption(
            'TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter',
            \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
            $dateFormat);
}
if (isset($this->arguments['paymentperiod'])) {
    $this->arguments['paymentperiod']
        ->getPropertyMappingConfiguration()
        ->forProperty('stopdate')
        ->setTypeConverterOption(
            'TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter',
            \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
            $dateFormat);
}
}

/**
 * action new
 *
 * @param \NGiB\Ngibmembers\Domain\Model\Paymentperiod $paymentperiod
 * @ignorevalidation $paymentperiod
 * @return void
 */
public function newAction(\NGiB\Ngibmembers\Domain\Model\Paymentperiod $paymentperiod = NULL) {
    $this->view->assign('paymentperiod',$paymentperiod);
    $this->view->assign('settings',$this->settings);
}

/**
 * action create
 *
 * @param \NGiB\Ngibmembers\Domain\Model\Paymentperiod $paymentperiod
 * @return void
 */
public function createAction(Paymentperiod $paymentperiod) {
    $this->paymentRepository->add($paymentperiod);
    $this->redirect('new');
}

and then the snippets from the fluid template:

<f:form action="create" object="{paymentperiod}" class="form-horizontal" name="paymentperiod">

<f:form.textfield
    id="paymentperiod_field_startdate"
    property="startdate"
    class="form-control" />

so, where is my problem????

Many thanks for everyone who helps.

Best regards


回答1:


ok, found the problem:

so the bootstrap datepicker I chose needs 'dd.mm.yyyy' in the dateformat to give a correct central european formatted date.

After some more debugging and reading, I finally found out, that the PHP DateTime class uses 'd.m.Y' to resolve the same format.

changing the dateFormat in the initializeAction of the controller solved the validation problem.

I definitely need to get a better datepicker library there :S



来源:https://stackoverflow.com/questions/31951582/typo3-extbase-persisting-domain-model-with-datetime-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!