FLOW3: Convert a date string from an input field of a view into a DateTime object

As I’ve mentioned earlier, the FLOW3 documentation is a bit weak at the moment. The initial example walks you through a very simple view where only strings (like “title”, “author”) are entered via the view’s form. But how do you enter a date and have it converted to a “real” \DateTime upon initializing the object that should contain it?

I found the solution (again, mainly by chance) here: How to use the DateTimeConverter in FLOW3. Apparently, you have to convert the user’s input to the needed objects in a method in the corresponding controller, that is (magically?) called before the method that expects the main object you want to create (that contains the date).

Here’s an example: If you have a Report object that has a dueDate attribute of type \DateTime, you have to convert the user input (a string) to a \DateTime object in the method initializeCreateAction() of class ReportController.

FLOW3 offers quite a few type converters, among which you can also find a DateTimeConverter. You can use it like this in initializeCreateAction():

public function createAction(Report $report) { ... } public function initializeCreateAction() { $this->arguments['report'] ->getPropertyMappingConfiguration() ->forProperty('dueDate') ->setTypeConverterOption( 'TYPO3\FLOW3\Property\TypeConverter\DateTimeConverter', \TYPO3\FLOW3\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'Y-m-d'); }

FLOW3 now converts the input string (format ‘Y-m-d’) into a “real” \DateTime object before passing it on to the newly created Report object, that gets passed to createAction().

Über Stefan

Polyglot Clean Code Developer

Ein Kommentar

  1. Vielen Dank, genau das habe ich gesucht mit dem Datum! Klappt prima!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax