Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add range field and support step in text field
  • Loading branch information
fritzmg committed Jan 4, 2020
1 parent fa552ce commit b6a2ef2
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions core-bundle/src/Resources/contao/config/config.php
Expand Up @@ -252,6 +252,7 @@
'radio' => 'Contao\FormRadioButton',
'checkbox' => 'Contao\FormCheckBox',
'upload' => 'Contao\FormFileUpload',
'range' => 'Contao\FormRange',
'hidden' => 'Contao\FormHidden',
'captcha' => 'Contao\FormCaptcha',
'submit' => 'Contao\FormSubmit',
Expand Down
10 changes: 9 additions & 1 deletion core-bundle/src/Resources/contao/dca/tl_form_field.php
Expand Up @@ -104,13 +104,14 @@
'fieldsetStop' => '{type_legend},type;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'html' => '{type_legend},type;{text_legend},html;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'text' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'textdigit' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minval,maxval,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'textdigit' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minval,maxval,step,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'password' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'textarea' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{size_legend},size;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'select' => '{type_legend},type,name,label;{fconfig_legend},mandatory,multiple;{options_legend},options;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'radio' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{options_legend},options;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'checkbox' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{options_legend},options;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'upload' => '{type_legend},type,name,label;{fconfig_legend},mandatory,extensions,maxlength;{store_legend:hide},storeFile;{expert_legend:hide},class,accesskey,tabindex,fSize;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'range' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{expert_legend:hide},class,value,minlength,maxlength,step,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'hidden' => '{type_legend},type,name,value;{fconfig_legend},mandatory,rgxp;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'captcha' => '{type_legend},type,label;{fconfig_legend},placeholder;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible',
'submit' => '{type_legend},type,slabel;{image_legend:hide},imageSubmit;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible'
Expand Down Expand Up @@ -250,6 +251,13 @@
'inputType' => 'text',
'eval' => array('rgxp'=>'natural', 'tl_class'=>'w50'),
'sql' => "varchar(10) NOT NULL default ''"
'step' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_form_field']['step'],
'exclude' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'natural', 'tl_class'=>'w50'),
'sql' => "int(10) unsigned NOT NULL default 0"
),
'size' => array
(
Expand Down
160 changes: 160 additions & 0 deletions core-bundle/src/Resources/contao/forms/FormRange.php
@@ -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');
10 changes: 10 additions & 0 deletions core-bundle/src/Resources/contao/forms/FormTextField.php
Expand Up @@ -112,6 +112,16 @@ public function __set($strKey, $varValue)
break;

case 'step':
if ($varValue > 0 && $this->type == 'number')
{
$this->arrAttributes[$strKey] = $varValue;
}
else
{
unset($this->arrAttributes[$strKey]);
}
break;

case 'placeholder':
$this->arrAttributes[$strKey] = $varValue;
break;
Expand Down
Expand Up @@ -139,6 +139,11 @@
</trans-unit>
<trans-unit id="tl_form_field.maxval.1">
<source>Here you can set the maximum value for numeric fields.</source>
<trans-unit id="tl_form_field.step.0">
<source>Step</source>
</trans-unit>
<trans-unit id="tl_form_field.step.1">
<source>The amount at which the number of the field increments or decrements at each step.</source>
</trans-unit>
<trans-unit id="tl_form_field.size.0">
<source>Rows and columns</source>
Expand Down
21 changes: 21 additions & 0 deletions core-bundle/src/Resources/contao/templates/forms/form_range.html5
@@ -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(); ?>

0 comments on commit b6a2ef2

Please sign in to comment.