__('Password', __FILE__), // Module Title 'summary' => __("Password input with confirmation field that doesn't ever echo the input back.", __FILE__), // Module Summary 'version' => 101, 'permanent' => true, ); } protected $_page = null; public function __construct() { parent::__construct(); $this->attr('type', 'password'); $this->attr('size', 50); $this->attr('maxlength', 256); $this->set('minlength', 6); } /** * Sets the page being edited, not always applicable * * @param Page $page * */ public function setPage(Page $page) { $this->_page = $page; if($page->is(Page::statusUnpublished)) $this->required = true; } public function ___render() { $this->label = $this->_('Set Password'); if(!$this->description) $this->set('description', sprintf($this->_("Password must be at least %d characters and have at least 1 letter and 1 digit. Password may not have whitespace."), $this->minlength)); // Description of valid password $value = $this->attr('value'); $trackChanges = $this->trackChanges(); if($trackChanges) $this->setTrackChanges(false); $this->attr('value', ''); $out = "\n

getAttributesString() . " />

" . "\n

" . $this->_('(confirm)') . "

"; $this->attr('value', $value); if($trackChanges) $this->setTrackChanges(true); return $out; } public function set($key, $value) { if($key == 'collapsed' && $this->_page && $this->_page->is(Page::statusUnpublished)) { // prevent collapse of field when pass is for unpublished user $value = Inputfield::collapsedNo; } return parent::set($key, $value); } public function ___renderValue() { return strlen($this->attr('value')) ? '******' : ''; } public function ___processInput(WireInputData $input) { parent::___processInput($input); $key = $this->attr('name'); $value = $this->attr('value'); if(isset($input->$key)) { // form was submitted if($input->$key) { // password was submitted $confirmKey = "_" . $key; if(preg_match('/[\s\t\r\n]/', $value)) $this->error($this->_("Password contained whitespace.")); if($input->$confirmKey != $input->$key) $this->error($this->_("Passwords do not match.")); if(strlen($value) < $this->minlength) $this->error($this->_("Password is less than required number of characters.")); if(!preg_match('/[a-zA-Z]/', $value)) $this->error($this->_("Password does not contain at least one letter (a-z A-Z).")); if(!preg_match('/\d/', $value)) $this->error($this->_("Password does not contain at least one digit (0-9).")); } else if($this->required) { $this->error($this->_("Required password was not specified.")); } if(count($this->getErrors())) { $this->attr('value', ''); $this->resetTrackChanges(); // don't record a change } } return $this; } /** * Return the fields required to configure an instance of InputfieldPassword * */ public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $skips = array('collapsed', 'placeholder', 'stripTags', 'pattern'); foreach($skips as $name) { $f = $inputfields->get('collapsed'); if($f) $inputfields->remove($f); } return $inputfields; } }