diff --git a/wire/core/Inputfield.php b/wire/core/Inputfield.php index 0fb6fd36..1ff3cfbf 100644 --- a/wire/core/Inputfield.php +++ b/wire/core/Inputfield.php @@ -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; } /** @@ -1492,6 +1495,22 @@ abstract class Inputfield extends WireData implements Module { $field->description = $this->_("If checked, a value will be required for this field."); $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'); @@ -1569,6 +1588,7 @@ abstract class Inputfield extends WireData implements Module { 'columnWidth', 'required', 'requiredIf', + 'requiredAttr', 'showIf' ); } diff --git a/wire/modules/Inputfield/InputfieldText.module b/wire/modules/Inputfield/InputfieldText.module index a914901c..a51a215c 100644 --- a/wire/modules/Inputfield/InputfieldText.module +++ b/wire/modules/Inputfield/InputfieldText.module @@ -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');