1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-14 10:45:54 +02:00

Move the requiredAttr Inputfield config option from InputfieldText into Inputfield so that it can be used on other Inputfield types

This commit is contained in:
Ryan Cramer
2020-02-28 16:38:55 -05:00
parent 60d62ea3a1
commit f584ec5317
2 changed files with 24 additions and 25 deletions

View File

@@ -87,6 +87,7 @@
* ===================
* @property int|bool $required Set to true (or 1) to make input required, or false (or 0) to make not required (default=0). #pw-group-behavior
* @property string $requiredIf Optional conditions under which input is required (selector string). #pw-group-behavior
* @property int|bool|null $requiredAttr Use HTML5 “required” attribute when used by Inputfield and $required is true? Default=null. #pw-group-behavior
* @property InputfieldWrapper|null $parent The parent InputfieldWrapper for this Inputfield or null if not set. #pw-internal
* @property null|bool|Fieldtype $hasFieldtype The Fieldtype using this Inputfield, or boolean false when known not to have a Fieldtype, or null when not known. #pw-group-other
* @property null|Field $hasField The Field object associated with this Inputfield, or null when not applicable or not known. #pw-group-other
@@ -878,11 +879,13 @@ abstract class Inputfield extends WireData implements Module {
*
*/
public function getAttributes() {
$attributes = array();
foreach($this->attributes as $key => $value) {
$attributes[$key] = $value;
$attrs = $this->attributes;
if(!isset($attrs['required']) && $this->getSetting('required') && $this->getSetting('requiredAttr')) {
if(!$this->getSetting('showIf') && !$this->getSetting('requiredIf')) {
$attrs['required'] = 'required';
}
return $attributes;
}
return $attrs;
}
/**
@@ -1493,6 +1496,22 @@ abstract class Inputfield extends WireData implements Module {
$field->collapsed = $this->getSetting('required') ? Inputfield::collapsedNo : Inputfield::collapsedYes;
$fields->add($field);
$requiredAttr = $this->getSetting('requiredAttr');
if($requiredAttr !== null) {
// Inputfield must have set requiredAttr to some non-null value before this will appear as option in config
$field->columnWidth = 50; // required checkbox
/** @var InputfieldCheckbox $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'requiredAttr');
$f->label = $this->_('Also use HTML5 “required” attribute?');
$f->showIf = "required=1, showIf='', requiredIf=''";
$f->description = $this->_('Use only on fields *always* visible to the user.');
$f->icon = 'html5';
$f->columnWidth = 50;
if($requiredAttr) $f->attr('checked', 'checked');
$fields->add($f);
}
/** @var InputfieldText $field */
$field = $this->modules->get('InputfieldText');
$field->label = $this->_('Required only if');
@@ -1569,6 +1588,7 @@ abstract class Inputfield extends WireData implements Module {
'columnWidth',
'required',
'requiredIf',
'requiredAttr',
'showIf'
);
}

View File

@@ -136,10 +136,6 @@ class InputfieldText extends Inputfield {
$attrs['value'] = $this->initValue;
}
if($this->required && $this->requiredAttr && !$this->getSetting('showIf') && !$this->getSetting('requiredIf')) {
$attrs['required'] = 'required';
}
if(isset($attrs['minlength'])) {
// convert minlength attr to a data-minlength attr, since minlength doesn't have wide browser support
if($attrs['minlength'] > 0) $attrs['data-minlength'] = $attrs['minlength'];
@@ -278,23 +274,6 @@ class InputfieldText extends Inputfield {
$inputfields = parent::___getConfigInputfields();
/** @var InputfieldCheckbox $required */
$required = $inputfields->getChildByName('required');
if($required) {
$required->set('columnWidth', 50);
/** @var InputfieldCheckbox $field */
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'requiredAttr');
$field->label = $this->_('Also use HTML5 “required” attribute?');
$field->showIf = "required=1, showIf='', requiredIf=''";
$field->description = $this->_('Use only on fields *always* visible to the user.');
$field->icon = 'html5';
$field->columnWidth = 50;
if($this->requiredAttr) $field->attr('checked', 'checked');
$required->getParent()->insertAfter($field, $required);
}
/** @var InputfieldInteger $field */
$field = $this->modules->get('InputfieldInteger');
$field->setAttribute('name', 'minlength');