__('Text', __FILE__), // Module Title
'summary' => __('Single line of text', __FILE__), // Module Summary
'version' => 105,
'permanent' => true,
);
}
public function __construct() {
parent::__construct();
$this->setAttribute('type', 'text');
$this->setAttribute('size', 0);
$this->setAttribute('maxlength', self::defaultMaxlength);
$this->setAttribute('placeholder', '');
$this->setAttribute('pattern', '');
$this->set('initValue', ''); // optional initial value
$this->set('stripTags', false); // strip tags from input?
}
public function ___render() {
$out = "\ngetAttributesString() . " />";
return $out;
}
public function getAttributes() {
$attrs = parent::getAttributes();
if(empty($attrs['size'])) {
unset($attrs['size']);
$attrs['class'] = (empty($attrs['class']) ? '' : $attrs['class'] . ' ') . 'InputfieldMaxWidth';
}
if(!strlen($attrs['value']) && $this->initValue) {
$attrs['value'] = $this->initValue;
}
// Remove HTML5 client-side validation required attribute until browsers implement it better
// if($this->required && $this->hasFieldtype === false) $attrs['required'] = 'required';
return $attrs;
}
public function setAttribute($key, $value) {
if($key == 'maxlength' && ((int) $value) < 1) $value = self::defaultMaxlength; // blank string prevents a maxlength='0' attribute
if($key == 'value') $value = $this->setAttributeValue($value);
return parent::setAttribute($key, $value);
}
protected function setAttributeValue($value) {
if($this->maxlength) {
$value = wire('sanitizer')->text($value, array(
'maxLength' => $this->maxlength,
'maxBytes' => $this->maxlength*3,
'stripTags' => false,
));
}
if($this->stripTags) $value = strip_tags($value);
return $value;
}
public function ___processInput(WireInputData $input) {
parent::___processInput($input);
$value = $this->attr('value');
if($this->pattern && strlen($value)) {
$regex = '#' . str_replace('#', '\#', $this->pattern) . '#'; // add delimeters
if(!preg_match($regex, $value)) $this->error($this->_('Does not match required pattern'));
}
return $this;
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$field = $this->modules->get('InputfieldInteger');
$field->setAttribute('name', 'size');
$field->label = $this->_('Size');
$field->setAttribute('value', $this->attr('size') > 0 ? $this->attr('size') : 0);
$field->setAttribute('size', 4);
$field->description = $this->_('The displayed width of this field (in characters). Set to 0 for full width.');
$field->collapsed = Inputfield::collapsedYes;
$inputfields->append($field);
$field = $this->modules->get('InputfieldInteger');
$field->setAttribute('name', 'maxlength');
$field->label = $this->_('Maxlength');
$field->setAttribute('value', $this->attr('maxlength'));
$field->setAttribute('size', 6);
$field->description = $this->_('The maximum length (in characters) that are allowed by this field.');
$field->collapsed = Inputfield::collapsedYes;
$inputfields->append($field);
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'stripTags');
$field->label = $this->_('Strip Tags');
$field->description = $this->_('When checked, any HTML tags will be stripped from the input when the form is processed.');
$field->notes = $this->_('This is recommended if the field does not need to support HTML in it.');
$field->attr('value', 1);
if($this->stripTags) $field->attr('checked', 'checked');
else $field->collapsed = Inputfield::collapsedYes;
$inputfields->append($field);
$field = $this->modules->get('InputfieldText');
$field->setAttribute('name', 'placeholder');
$field->label = $this->_('Placeholder Text');
$field->setAttribute('value', $this->attr('placeholder'));
$field->description = $this->_('Optional placeholder phrase of text that appears in the field when blank.');
$field->collapsed = Inputfield::collapsedBlank;
$inputfields->append($field);
$field = $this->modules->get('InputfieldText');
$field->setAttribute('name', 'pattern');
$field->label = $this->_('Pattern');
$field->setAttribute('value', $this->attr('pattern'));
$field->description = $this->_('Optional regular expression pattern to require in the input. This is used both client side (HTML5 pattern attribute) and server side for validation. Be sure to provide an example of the required pattern in your field description.'); // Pattern description
$field->notes = $this->_('See [html5pattern.com](http://html5pattern.com) for examples of patterns you can use and create.'); // Pattern notes
$field->collapsed = Inputfield::collapsedBlank;
$inputfields->append($field);
if($this->hasFieldtype === false) {
$field = $this->modules->get('InputfieldText');
$field->setAttribute('name', 'initValue');
$field->label = $this->_('Initial Value');
$field->description = $this->_('Optional initial/default value pre-populated for the user.');
$field->setAttribute('value', $this->initValue);
$field->collapsed = Inputfield::collapsedBlank;
$inputfields->append($field);
}
return $inputfields;
}
}