mirror of
https://github.com/processwire/processwire.git
synced 2025-08-11 01:04:16 +02:00
Minor code updates to FieldtypeEmail and InputfieldEmail
This commit is contained in:
@@ -22,43 +22,91 @@ class FieldtypeEmail extends FieldtypeText {
|
|||||||
'summary' => 'Field that stores an e-mail address',
|
'summary' => 'Field that stores an e-mail address',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get max email address length
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function getMaxEmailLength() {
|
public function getMaxEmailLength() {
|
||||||
return $this->wire('database')->getMaxIndexLength();
|
return $this->wire()->database->getMaxIndexLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Inputfield module for this Fieldtype
|
||||||
|
*
|
||||||
|
* @param Page $page
|
||||||
|
* @param Field $field
|
||||||
|
* @return InputfieldEmail
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function getInputfield(Page $page, Field $field) {
|
public function getInputfield(Page $page, Field $field) {
|
||||||
/** @var InputfieldEmail $inputfield */
|
/** @var InputfieldEmail $inputfield */
|
||||||
$inputfield = $this->modules->get('InputfieldEmail');
|
$inputfield = $this->wire()->modules->get('InputfieldEmail');
|
||||||
$inputfield->class = $this->className();
|
$inputfield->addClass($this->className());
|
||||||
return $inputfield;
|
return $inputfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize value for page
|
||||||
|
*
|
||||||
|
* @param Page $page
|
||||||
|
* @param Field $field
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function sanitizeValue(Page $page, Field $field, $value) {
|
public function sanitizeValue(Page $page, Field $field, $value) {
|
||||||
if(strlen($value) > $this->getMaxEmailLength()) return '';
|
if(strlen($value) > $this->getMaxEmailLength()) return '';
|
||||||
return $this->wire('sanitizer')->email($value);
|
return $this->wire()->sanitizer->email($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get database schema for field
|
||||||
|
*
|
||||||
|
* @param Field $field
|
||||||
|
* @return array
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function getDatabaseSchema(Field $field) {
|
public function getDatabaseSchema(Field $field) {
|
||||||
$len = $this->getMaxEmailLength();
|
$len = $this->getMaxEmailLength();
|
||||||
$schema = parent::getDatabaseSchema($field);
|
$schema = parent::getDatabaseSchema($field);
|
||||||
$schema['data'] = "varchar($len) NOT NULL default ''";
|
$schema['data'] = "varchar($len) NOT NULL default ''";
|
||||||
|
|
||||||
if($field->hasFlag(Field::flagUnique) != (bool) $field->flagUnique) {
|
if($field->hasFlag(Field::flagUnique) != (bool) $field->flagUnique) {
|
||||||
$fields = $this->wire('fields'); /** @var Fields $fields */
|
if($this->wire()->getStatus() >= ProcessWire::statusReady) {
|
||||||
$fields->tableTools()->checkUniqueIndex($field);
|
$fields = $this->wire()->fields;
|
||||||
|
$fields->tableTools()->checkUniqueIndex($field);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $schema;
|
return $schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is given value one that doesn’t need to be stored in DB?
|
||||||
|
*
|
||||||
|
* @param Page $page
|
||||||
|
* @param Field $field
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function isDeleteValue(Page $page, Field $field, $value) {
|
public function isDeleteValue(Page $page, Field $field, $value) {
|
||||||
return empty($value);
|
return empty($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advanced configuration for field
|
||||||
|
*
|
||||||
|
* @param Field $field
|
||||||
|
* @return InputfieldWrapper
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___getConfigAdvancedInputfields(Field $field) {
|
public function ___getConfigAdvancedInputfields(Field $field) {
|
||||||
$inputfields = parent::___getConfigAdvancedInputfields($field);
|
$inputfields = parent::___getConfigAdvancedInputfields($field);
|
||||||
$fields = $this->wire('fields'); /** @var Fields $fields */
|
$fields = $this->wire()->fields;
|
||||||
$f = $fields->tableTools()->getUniqueIndexInputfield($field);
|
$f = $fields->tableTools()->getUniqueIndexInputfield($field);
|
||||||
$inputfields->prepend($f);
|
$inputfields->prepend($f);
|
||||||
return $inputfields;
|
return $inputfields;
|
||||||
|
@@ -1,11 +1,16 @@
|
|||||||
<?php namespace ProcessWire;
|
<?php namespace ProcessWire;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An Inputfield for handling E-Mail addresses
|
* An Inputfield for handling email addresses
|
||||||
*
|
*
|
||||||
|
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
|
||||||
|
* https://processwire.com
|
||||||
|
*
|
||||||
* @property int $confirm Specify 1 to make it include a second input for confirmation
|
* @property int $confirm Specify 1 to make it include a second input for confirmation
|
||||||
* @property string $confirmLabel label to accompany second input
|
* @property string $confirmLabel label to accompany second input
|
||||||
* @property int maxlength Max length of email address (default=512)
|
* @property int maxlength Max length of email address (default=512)
|
||||||
|
*
|
||||||
|
* @method string renderConfirm(array $attrs)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class InputfieldEmail extends InputfieldText {
|
class InputfieldEmail extends InputfieldText {
|
||||||
@@ -15,9 +20,13 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
'title' => __('Email', __FILE__), // Module Title
|
'title' => __('Email', __FILE__), // Module Title
|
||||||
'version' => 101,
|
'version' => 101,
|
||||||
'summary' => __('E-Mail address in valid format', __FILE__) // Module Summary
|
'summary' => __('E-Mail address in valid format', __FILE__) // Module Summary
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->setAttribute('name', 'email');
|
$this->setAttribute('name', 'email');
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@@ -29,30 +38,70 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
$this->set('value2', '');
|
$this->set('value2', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render input
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___render() {
|
public function ___render() {
|
||||||
if(!$this->label || $this->label == $this->name) $this->label = $this->_('E-Mail'); // label headline when no default specified
|
|
||||||
if($this->confirm && count($this->getErrors())) $this->attr('value', '');
|
if(!$this->label || $this->label === $this->attr('name')) {
|
||||||
$attrs = $this->getAttributes();
|
$this->label = $this->_('E-Mail'); // label headline when no default specified
|
||||||
$out = "<input " . $this->getAttributesString($attrs) . " />";
|
|
||||||
if($this->confirm) {
|
|
||||||
foreach(array('id', 'name') as $key) {
|
|
||||||
if(isset($attrs[$key])) $attrs[$key] = '_' . $attrs[$key] . '_confirm';
|
|
||||||
}
|
|
||||||
$attrs['aria-label'] = $this->confirmLabel;
|
|
||||||
$attrs['placeholder'] = $this->confirmLabel;
|
|
||||||
$out .= "<div style='margin-top: 0.5em;'><input " . $this->getAttributesString($attrs) . " /></div>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($this->confirm && count($this->getErrors())) $this->val('');
|
||||||
|
|
||||||
|
$attrs = $this->getAttributes();
|
||||||
|
|
||||||
|
$out = "<input " . $this->getAttributesString($attrs) . " />";
|
||||||
|
|
||||||
|
if($this->confirm) $out .= $this->renderConfirm($attrs);
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the seoondary “Confirm” email input
|
||||||
|
*
|
||||||
|
* @param array $attrs
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function ___renderConfirm(array $attrs) {
|
||||||
|
|
||||||
|
foreach(array('id', 'name') as $key) {
|
||||||
|
if(isset($attrs[$key])) $attrs[$key] = '_' . $attrs[$key] . '_confirm';
|
||||||
|
}
|
||||||
|
|
||||||
|
$attrs['aria-label'] = $this->confirmLabel;
|
||||||
|
$attrs['placeholder'] = $this->confirmLabel;
|
||||||
|
|
||||||
|
return "<div style='margin-top:0.5em'><input " . $this->getAttributesString($attrs) . " /></div>";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set attribute value
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
protected function setAttributeValue($value) {
|
protected function setAttributeValue($value) {
|
||||||
if(strlen($value)) {
|
if(strlen($value)) {
|
||||||
$value = $this->wire('sanitizer')->email($value);
|
$value = $this->wire()->sanitizer->email($value);
|
||||||
if(!$value) $this->error($this->_("Please enter a valid e-mail address")); // Error message when email address is invalid
|
if(!strlen($value)) $this->error($this->_("Please enter a valid e-mail address")); // Error message when email address is invalid
|
||||||
}
|
}
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process input
|
||||||
|
*
|
||||||
|
* @param WireInputData $input
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___processInput(WireInputData $input) {
|
public function ___processInput(WireInputData $input) {
|
||||||
|
|
||||||
$field = $this->hasField;
|
$field = $this->hasField;
|
||||||
@@ -60,6 +109,7 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
$page = $this->hasPage;
|
$page = $this->hasPage;
|
||||||
$errors = array();
|
$errors = array();
|
||||||
$valuePrevious = $this->val();
|
$valuePrevious = $this->val();
|
||||||
|
$name = $this->attr('name');
|
||||||
|
|
||||||
parent::___processInput($input);
|
parent::___processInput($input);
|
||||||
|
|
||||||
@@ -67,15 +117,15 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
$changed = strtolower($value) !== strtolower($valuePrevious);
|
$changed = strtolower($value) !== strtolower($valuePrevious);
|
||||||
|
|
||||||
if($this->confirm) {
|
if($this->confirm) {
|
||||||
$value2 = $this->wire('sanitizer')->email($input["_{$this->name}_confirm"]);
|
$value2 = $this->wire()->sanitizer->email($input["_{$name}_confirm"]);
|
||||||
if((strlen($value) || strlen($value2)) && strtolower($value) !== strtolower($value2)) {
|
if((strlen($value) || strlen($value2)) && strtolower($value) !== strtolower($value2)) {
|
||||||
$errors[] = $this->_('The emails you entered did not match, please enter again');
|
$errors[] = $this->_('The emails you entered did not match, please enter again');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($changed && $value && $field && $page && $field->hasFlag(Field::flagUnique)) {
|
if($changed && $value && $field && $page && $field->hasFlag(Field::flagUnique)) {
|
||||||
$fields = $this->wire('fields'); /** @var Fields $fields */
|
$fields = $this->wire()->fields;
|
||||||
$pageId = $fields->tableTools()->valueExists($this->hasField, $value);
|
$pageId = $fields->tableTools()->valueExists($field, $value);
|
||||||
if($pageId && $pageId != $page->id) {
|
if($pageId && $pageId != $page->id) {
|
||||||
$errors[] = sprintf($this->_('Email “%s” is already in use, please use a different one'), $value);
|
$errors[] = sprintf($this->_('Email “%s” is already in use, please use a different one'), $value);
|
||||||
}
|
}
|
||||||
@@ -96,17 +146,24 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Field config
|
||||||
|
*
|
||||||
|
* @return InputfieldWrapper
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___getConfigInputfields() {
|
public function ___getConfigInputfields() {
|
||||||
|
|
||||||
$inputfields = parent::___getConfigInputfields();
|
$inputfields = parent::___getConfigInputfields();
|
||||||
|
|
||||||
$skips = array('stripTags', 'pattern');
|
$skips = array('stripTags', 'pattern');
|
||||||
|
|
||||||
foreach($skips as $name) {
|
foreach($skips as $name) {
|
||||||
$f = $inputfields->get($name);
|
$f = $inputfields->get($name);
|
||||||
if($f) $inputfields->remove($f);
|
if($f) $inputfields->remove($f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var InputfieldCheckbox $f */
|
/** @var InputfieldCheckbox $f */
|
||||||
$f = $this->wire('modules')->get('InputfieldCheckbox');
|
$f = $this->wire()->modules->get('InputfieldCheckbox');
|
||||||
$f->attr('name', 'confirm');
|
$f->attr('name', 'confirm');
|
||||||
$f->label = $this->_('Confirm email address?');
|
$f->label = $this->_('Confirm email address?');
|
||||||
$f->description = $this->_('When checked, two email inputs will appear and the user will have to enter their email address twice to confirm it. This helps reduce the possibility of typos.');
|
$f->description = $this->_('When checked, two email inputs will appear and the user will have to enter their email address twice to confirm it. This helps reduce the possibility of typos.');
|
||||||
@@ -114,6 +171,7 @@ class InputfieldEmail extends InputfieldText {
|
|||||||
$f->collapsed = $this->confirm ? Inputfield::collapsedNo : Inputfield::collapsedYes;
|
$f->collapsed = $this->confirm ? Inputfield::collapsedNo : Inputfield::collapsedYes;
|
||||||
if($this->confirm) $f->attr('checked', 'checked');
|
if($this->confirm) $f->attr('checked', 'checked');
|
||||||
$inputfields->add($f);
|
$inputfields->add($f);
|
||||||
|
|
||||||
return $inputfields;
|
return $inputfields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user