Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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