forked from contao/contao
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add range field and support step in text field
- Loading branch information
Showing
6 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao; | ||
|
||
/** | ||
* Class FormRange | ||
* | ||
* @property string $value | ||
* @property string $type | ||
* @property boolean $mandatory | ||
* @property integer $min | ||
* @property integer $max | ||
* @property integer $step | ||
* | ||
* @author Fritz Michael Gschwantner <https://github.com/fritzmg> | ||
*/ | ||
class FormRange extends Widget | ||
{ | ||
|
||
/** | ||
* Submit user input | ||
* | ||
* @var boolean | ||
*/ | ||
protected $blnSubmitInput = true; | ||
|
||
/** | ||
* Add a for attribute | ||
* | ||
* @var boolean | ||
*/ | ||
protected $blnForAttribute = true; | ||
|
||
/** | ||
* Template | ||
* | ||
* @var string | ||
*/ | ||
protected $strTemplate = 'form_range'; | ||
|
||
/** | ||
* The CSS class prefix | ||
* | ||
* @var string | ||
*/ | ||
protected $strPrefix = 'widget widget-range'; | ||
|
||
/** | ||
* Add specific attributes | ||
* | ||
* @param string $strKey The attribute key | ||
* @param mixed $varValue The attribute value | ||
*/ | ||
public function __set($strKey, $varValue) | ||
{ | ||
switch ($strKey) | ||
{ | ||
// Treat minlength/minval as min for number type field (#1622) | ||
case 'minlength': | ||
case 'minval': | ||
$this->min = $varValue; | ||
break; | ||
|
||
// Treat maxlength/maxval as max for number type field (#1622) | ||
case 'maxlength': | ||
case 'maxval': | ||
$this->max = $varValue; | ||
break; | ||
|
||
case 'mandatory': | ||
if ($varValue) | ||
{ | ||
$this->arrAttributes['required'] = 'required'; | ||
} | ||
else | ||
{ | ||
unset($this->arrAttributes['required']); | ||
} | ||
parent::__set($strKey, $varValue); | ||
break; | ||
|
||
case 'min': | ||
case 'max': | ||
if ($varValue > 0) | ||
{ | ||
$this->arrAttributes[$strKey] = $varValue; | ||
$this->arrConfiguration[$strKey.'val'] = $varValue; | ||
} | ||
else | ||
{ | ||
unset($this->arrAttributes[$strKey], $this->arrConfiguration[$strKey.'val']); | ||
} | ||
unset($this->arrAttributes[$strKey.'length']); | ||
break; | ||
|
||
case 'step': | ||
if ($varValue > 0) | ||
{ | ||
$this->arrAttributes[$strKey] = $varValue; | ||
} | ||
else | ||
{ | ||
unset($this->arrAttributes[$strKey]); | ||
} | ||
break; | ||
|
||
default: | ||
parent::__set($strKey, $varValue); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Return a parameter. | ||
* | ||
* @param string $strKey The parameter key | ||
* | ||
* @return mixed The parameter value | ||
*/ | ||
public function __get($strKey) | ||
{ | ||
switch ($strKey) { | ||
case 'type': | ||
return 'range'; | ||
break; | ||
|
||
default: | ||
return parent::__get($strKey); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Generate the widget and return it as string. | ||
* | ||
* @return string The widget markup | ||
*/ | ||
public function generate() | ||
{ | ||
return sprintf('<input type="%s" name="%s" id="ctrl_%s" class="range%s%s" value="%s"%s%s', | ||
$this->type, | ||
$this->strName, | ||
$this->strId, | ||
($this->hideInput ? ' password' : ''), | ||
(('' !== $this->strClass) ? ' '.$this->strClass : ''), | ||
StringUtil::specialchars($this->value), | ||
$this->getAttributes(), | ||
$this->strTagEnding); | ||
} | ||
} | ||
|
||
class_alias(FormRange::class, 'FormRange'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core-bundle/src/Resources/contao/templates/forms/form_range.html5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php $this->extend('form_row'); ?> | ||
|
||
<?php $this->block('label'); ?> | ||
<?php if ($this->label): ?> | ||
<label for="ctrl_<?= $this->id ?>"<?php if ($this->class): ?> class="<?= $this->class ?>"<?php endif; ?>> | ||
<?php if ($this->mandatory): ?> | ||
<span class="invisible"><?= $this->mandatoryField ?> </span><?= $this->label ?><span class="mandatory">*</span> | ||
<?php else: ?> | ||
<?= $this->label ?> | ||
<?php endif; ?> | ||
</label> | ||
<?php endif; ?> | ||
<?php $this->endblock(); ?> | ||
|
||
<?php $this->block('field'); ?> | ||
<?php if ($this->hasErrors()): ?> | ||
<p class="error"><?= $this->getErrorAsString() ?></p> | ||
<?php endif; ?> | ||
|
||
<input type="<?= $this->type ?>" name="<?= $this->name ?>" id="ctrl_<?= $this->id ?>" class="range<?php if ($this->class): ?> <?= $this->class ?><?php endif; ?>" value="<?= Contao\StringUtil::specialchars($this->value) ?>"<?= $this->getAttributes() ?>> | ||
<?php $this->endblock(); ?> |