Commit 25cfc169 authored by eike.starkmann's avatar eike.starkmann Committed by Susanne Moog
Browse files

[FEATURE] Add "required" argument to select view helper.

Change-Id: Ic27930c09d6c373364e6299e5031397218694903
Resolves: #81656
Releases: master
Reviewed-on: https://review.typo3.org/53303


Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: default avatarSascha Rademacher <sascha.rademacher+typo3@gmail.com>
Tested-by: default avatarSascha Rademacher <sascha.rademacher+typo3@gmail.com>
Reviewed-by: Susanne Moog's avatarSusanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog's avatarSusanne Moog <susanne.moog@typo3.org>
parent 45def4a7
.. include:: ../../Includes.txt
===============================================================
Feature: #81656 - Select view helper supports required argument
===============================================================
See :issue:`81656`
Description
===========
The select view helper supports the optional argument "required" according to this:
https://www.w3schools.com/tags/att_select_required.asp
Impact
======
Set the "required" argument like this:
.. code-block:: html
<f:form.select property="category" required="1" options="{categories}" />
The rendered html output is this:
.. code-block:: html
<select required="required" name="tx_ext[model][category]">
<option value="...">...</option>
</select>
.. index:: Fluid, Frontend, Backend
......@@ -108,6 +108,7 @@ class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFie
$this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
$this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
$this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
$this->registerArgument('required', 'boolean', 'If set no empty value is allowed.', false, false);
}
/**
......@@ -118,6 +119,9 @@ class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFie
*/
public function render()
{
if ($this->arguments['required']) {
$this->tag->addAttribute('required', 'required');
}
$name = $this->getName();
if ($this->arguments['multiple']) {
$this->tag->addAttribute('multiple', 'multiple');
......
......@@ -74,6 +74,33 @@ class SelectViewHelperTest extends ViewHelperBaseTestcase
$this->viewHelper->render();
}
/**
* @test
*/
public function selectShouldSetTheRequiredAttribute()
{
$this->tagBuilder->expects($this->exactly(2))->method('addAttribute')->withConsecutive(
['required', 'required'],
['name', 'myName']
);
$this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
$this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
$this->tagBuilder->expects($this->once())->method('render');
$this->arguments['options'] = [
'value1' => 'label1',
'value2' => 'label2'
];
$this->arguments['value'] = 'value2';
$this->arguments['name'] = 'myName';
$this->arguments['required'] = '1';
$this->injectDependenciesIntoViewHelper($this->viewHelper);
$this->viewHelper->_set('tag', $this->tagBuilder);
$this->viewHelper->initialize();
$this->viewHelper->render();
}
/**
* @test
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment