Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a range field and support steps in text fields #521

Merged
merged 13 commits into from Jan 8, 2020
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
14 changes: 11 additions & 3 deletions 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,minval,maxval,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 @@ -241,14 +242,21 @@
(
'exclude' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'natural', 'tl_class'=>'w50'),
'eval' => array('rgxp'=>'digit', 'tl_class'=>'w50'),
'sql' => "varchar(10) NOT NULL default ''"
),
'maxval' => array
(
'exclude' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'natural', 'tl_class'=>'w50'),
'eval' => array('rgxp'=>'digit', 'tl_class'=>'w50'),
'sql' => "varchar(10) NOT NULL default ''"
),
'step' => array
(
'exclude' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'digit', 'tl_class'=>'w50'),
'sql' => "varchar(10) NOT NULL default ''"
),
'size' => array
Expand Down
122 changes: 122 additions & 0 deletions core-bundle/src/Resources/contao/forms/FormRange.php
@@ -0,0 +1,122 @@
<?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)
{
case 'min':
case 'minval':
$this->arrAttributes['min'] = $varValue;
break;

case 'max':
case 'maxval':
$this->arrAttributes['max'] = $varValue;
break;

case 'mandatory':
if ($varValue)
{
$this->arrAttributes['required'] = 'required';
}
else
{
unset($this->arrAttributes['required']);
}
parent::__set($strKey, $varValue);
break;

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

default:
parent::__set($strKey, $varValue);
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" value="%s"%s%s',
$this->type,
$this->strName,
$this->strId,
($this->strClass ? ' ' . $this->strClass : ''),
StringUtil::specialchars($this->value),
$this->getAttributes(),
$this->strTagEnding
);
}
}
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
6 changes: 6 additions & 0 deletions core-bundle/src/Resources/contao/languages/en/default.xlf
Expand Up @@ -488,6 +488,12 @@
<trans-unit id="FFL.upload.1">
<source>A single-line input field to upload a local file to the server.</source>
</trans-unit>
<trans-unit id="FFL.range.0">
<source>Range slider</source>
</trans-unit>
<trans-unit id="FFL.range.1">
<source>A range slider to select a value or range of values between a specified min and max.</source>
</trans-unit>
<trans-unit id="FFL.hidden.0">
<source>Hidden field</source>
</trans-unit>
Expand Down
Expand Up @@ -140,6 +140,12 @@
<trans-unit id="tl_form_field.maxval.1">
<source>Here you can set the maximum value for numeric fields.</source>
</trans-unit>
<trans-unit id="tl_form_field.step.0">
<source>Step</source>
</trans-unit>
<trans-unit id="tl_form_field.step.1">
<source>Here you can set the discrete step size of the field.</source>
</trans-unit>
<trans-unit id="tl_form_field.size.0">
<source>Rows and columns</source>
</trans-unit>
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; ?>"<?php if ($this->value): ?> value="<?= (float) $this->value ?>"<?php endif; ?><?= $this->getAttributes() ?>>
<?php $this->endblock(); ?>