Skip to content

Instantly share code, notes, and snippets.

@Savageman
Created October 11, 2011 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Savageman/1277747 to your computer and use it in GitHub Desktop.
Save Savageman/1277747 to your computer and use it in GitHub Desktop.
Fuel widget password
<?php
namespace Cms;
class Widget_Password extends \Fieldset_Field {
/**
* @var \Cms\Fieldset_Field
*/
protected $password;
/**
* @var \Cms\Fieldset_Field
*/
protected $verification;
protected $display = array();
/**
*
* @param string $name
* @param string $label
* @param array $attributes
* @param array $rules
* @param \Fuel\Core\Fieldset $fieldset
* @return Cms\Widget_Date
*/
public function __construct($name, $label = '', array $attributes = array(), array $rules = array(), \Fuel\Core\Fieldset $fieldset) {
parent::__construct(uniqid(), '', array(), array(), $fieldset);
$this->display[] = new \Fieldset_Field($name, $label, $attributes, array(), $fieldset);
$this->display[] = new \Fieldset_Field($name.'_verification', $label.' (confirmation)', $attributes, array(), $fieldset);
// Purpose of empty widget is validation!
$this->password = $fieldset->add_field(new Widget_Empty($name, '', array(), $rules, $fieldset));
$this->verification = $fieldset->add_field(new Widget_Empty($name.'_verification', '', array(), array(), $fieldset))
->add_rule('match_field', $name);
}
public function add_rule($callback) {
// Relay all validation rules to the password
call_user_func_array(array($this->password, 'add_rule'), func_get_args());
if ($callback == 'required') {
call_user_func_array(array($this->verification, 'add_rule'), func_get_args());
}
}
/**
* How to display the field
* @return type
*/
public function build() {
return implode('', $this->display);
}
public function repopulate(array $input) {
list($password, $verification) = array($input[$this->password->name], $input[$this->verification->name]);
// Remember previous entered values
if (!empty($password)) {
$this->password->set_value($password);
$this->display[0]->set_value($password);
}
if (!empty($verification)) {
$this->verification->set_value($verification);
$this->display[1]->set_value($verification);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment