[TYPO3-mvc] Validation of non-mandatory DateTime fields

Steffen Ritter info at rs-websystems.de
Tue Apr 26 10:14:28 CEST 2011


Am 12.04.2011 11:20, schrieb François Suter:
> Hi all,
>
> I stumbled on a weird behavior of the DateTime data type. Assuming I
> declare a property as having to validate as a DateTime object, i.e.
>
> /**
> * Date and time of the doctor's examination
> *
> * @var DateTime $doctorDate
> * @validate DateTime
> */
> protected $doctorDate;
>
> If the date is wrong upon input (say somebody entered "xxxx"), I'll get
> a validation error and the input form gets reloaded with an empty value
> in the Date field. That's fine.
>
> However if I leave the field empty (which is supposed to be fine,
> because it's not mandatory) and the form gets reloaded because of some
> other input error, the field is now filled with the current date, which
> is not desirable. At which point in the process was the current date
> "injected" into my property? Is there any way to prevent this?
>
> Cheers
>
DateTime properties are always a mess...

I always work around them as (this case the date propert is int not 
DateTime, but works equally):

/**
  * date
  *
  * @var string $dateString
  * @validate 
RegularExpression(regularExpression=/([0-3][0-9]\.[0-1][0-9]\.[0-9]{4})^$/)
  */
protected $dateString;

public function setDateString($date) {
	$this->dateString = $date;
	$date = t3lib_div::intExplode('.',$date, 3);
	$date = $date[1] . '/' . $date[0] . '/' . $date[2];
	if (strtotime($date) !== false) { $this->setDate(strtotime($date)); }
}
	
public function getDateString() {
	if ($this->getDate() != 0) {
		return date('d.m.Y', $this->getDate());
	} else {
		return '';
	}
}
	


dateString is not stored/existent on database of course ;)


regards :)



More information about the TYPO3-project-typo3v4mvc mailing list