:navigation-title: form.select .. include:: /Includes.rst.txt .. _typo3-fluid-form-select: ======================================== Form.select ViewHelper `` ======================================== .. typo3:viewhelper:: form.select :source: /Global.json :display: tags,description,gitHubLink :noindex: .. include:: /_Includes/_ExtbaseFormViewHelpers.rst.txt .. contents:: Table of contents .. _typo3-fluid-form-select-usage: Select field with hardcoded options =================================== Using the :ref:`options ` parameter, you can pass a hardcoded array to the form.select ViewHelper. The key of that array is used as option key, and the value is used as human-readable name. .. tabs:: .. group-tab:: Fluid .. literalinclude:: _codesnippets/_SelectGender.html :caption: packages/my_extension/Resources/Private/Templates/User/GenderForm.html .. group-tab:: Controller The argument will have the key of the chosen entry as value. .. literalinclude:: _codesnippets/_SelectGender.php :caption: packages/my_extension/Classes/Controller/UserController.php .. _typo3-fluid-form-select-usage-array: Preselected field with options supplied by the controller ========================================================= Most options will be supplied by the controller. You can pass an associative array with the options to the view: .. tabs:: .. group-tab:: Fluid .. literalinclude:: _codesnippets/_SelectPayment.html :caption: packages/my_extension/Resources/Private/Templates/User/PaymentForm.html .. group-tab:: Controller The argument for the select field will have the key of the chosen entry as value. .. literalinclude:: _codesnippets/_SelectPayment.php :caption: packages/my_extension/Classes/Controller/UserController.php To pre-select a value, use the argument :ref:`value `. .. _typo3-fluid-form-select-usage-models: Select field for selecting (persisted) models ============================================= Consider we have the payment options from the previous example in the database and in an Extbase model: .. tabs:: .. group-tab:: Model .. literalinclude:: _codesnippets/_SelectPaymentMethod.php :caption: packages/my_extension/Classes/Domain/Model/PaymentMethod.php .. group-tab:: Controller In the controller we can get all payment methods from the `Repository `_ now and pass it to the view as variable: .. literalinclude:: _codesnippets/_SelectPaymentModelController.php :caption: packages/my_extension/Classes/Controller/UserController.php .. group-tab:: Fluid The Fluid template can be left unchanged even though we are dealing with a different data source: .. literalinclude:: _codesnippets/_SelectPayment.html :caption: packages/my_extension/Resources/Private/Templates/User/PaymentForm.html When the form gets submitted, the UID of the chosen model appears in the request data. Extbase will then map that uid back to the model for you. The ViewHelper will then use the model's UID as data submitted in the form and the result of method :php:`__toString()` as display text. .. note:: You can pass any array or :php:`\Traversable` object to the :ref:`options ` argument. This includes the result of `findBy()` methods on `Extbase repositories `_, :php-short:`\TYPO3\CMS\Extbase\Persistence\QueryResultInterface` and objects stored as relation in another object as :php-short:`\TYPO3\CMS\Extbase\Persistence\ObjectStorage`. .. _typo3-fluid-form-select-usage-models-optionLabelField: optionLabelField: Define another property of the model for the option label --------------------------------------------------------------------------- If the model to be displayed has no :php:`__toString()` method or you want to display the content of a different field, use option :ref:`optionLabelField ` .. code-block:: html The :ref:`options ` may contain an array or anything else `Traversable` including .. _typo3-fluid-form-select-usage-models-optionValueField: optionValueField: Define another property of the model as value --------------------------------------------------------------- If you are dealing with non-persisted models or Data-Transfer-Objects (DTO) there is no valid identifier that could be used to automatically map the user's selection in your controller. In this case, you must provide the name of the field to be used to identify the object: .. code-block:: html The controller action will then be called with the content of that field: .. code-block:: html // Variable $payment contains the paymentIdentifier from the model public function selectPaymentAction(String $payment): ResponseInterface .. _typo3-fluid-form-select-usage-propery: Binding the select field to an object property ============================================== When the surrounding `` uses `Property mapping `_ we can also map the selected option to the model: .. tabs:: .. group-tab:: Fluid .. literalinclude:: _codesnippets/_SelectPaymentProperty.html :caption: packages/my_extension/Resources/Private/Templates/User/PaymentForm.html .. group-tab:: User Model The user model could have the following fields .. literalinclude:: _codesnippets/_SelectPaymentUser.php :caption: packages/my_extension/Classes/Domain/Model/User.php .. group-tab:: Controller The controller action will then be called with the user object: .. code-block:: php public function selectPreferredPaymentAction(User $user): ResponseInterface .. _typo3-fluid-form-select-subviewHelper: Working with options and option groups ====================================== .. _typo3-fluid-form-select-option: Form.select.option ViewHelper `` ------------------------------------------------------ .. typo3:viewhelper:: form.select.option :source: /Global.json :display: tags,description,gitHubLink :noindex: The `` ViewHelper can be used to create a completely `` :html:`