first step in converting registration to quickform

This commit is contained in:
Christian Weiske 2010-05-06 14:21:13 +02:00
parent 26052021a2
commit 4285799c3f
3 changed files with 185 additions and 40 deletions

View File

@ -0,0 +1,117 @@
<?php
require_once 'HTML/QuickForm2/Element/InputText.php';
/**
* Text input element with pre-set text that vanishes when
* the user focuses it.
*
* Example:
* before:
* Name: [John Do| ]
* E-Mail: [Please type your email address]
*
* after:
* Name: [John Doe ]
* E-Mail: [| ]
*
* @author Christian Weiske <cweiske@php.net>
*/
class SemanticScuttle_QuickForm2_Element_BackgroundText
extends HTML_QuickForm2_Element_InputText
{
protected $btText = null;
protected $btClass = null;
/**
* Sets the background text to show when the text element is
* empty and not focused
*
* @param string $text Background text to set
*
* @return SemanticScuttle_QuickForm2_BackgroundText This object
*/
public function setBackgroundText($text)
{
$this->btText = $text;
$this->btUpdateAttributes();
return $this;
}
/**
* Sets the HTML class to use when the text element is
* empty and not focused
*
* @param string $class HTML class to set when the element
* is not focused
*
* @return SemanticScuttle_QuickForm2_BackgroundText This object
*
* @FIXME: Class to set when it is focused
*/
public function setBackgroundClass($class)
{
$this->btClass = $class;
$this->btUpdateAttributes();
return $this;
}
/**
* Updates the attributes array after.
* Used after setting the background text or background class.
*
* @return void
*/
protected function btUpdateAttributes()
{
if ($this->btText == '') {
//deactivate it
unset($this->attributes['onfocus']);
unset($this->attributes['onblur']);
return;
}
$this->attributes['onfocus']
= 'if (this.value == '
. json_encode((string)$this->btText)
. ') this.value = "";';
$this->attributes['onblur']
= 'if (this.value == "") this.value = '
. json_encode((string)$this->btText)
. ';';
//default when loading the form
//FIXME: use some special char to distinguish that
//value from a user inputted one (i.e. UTF-8 empty character)
if (!isset($this->attributes['value']) || !$this->attributes['value']) {
$this->attributes['value'] = $this->btText;
}
//FIXME: class
}
/**
* Called when the element needs to update its value
* from form's data sources.
* This method overwrites the parent one to skip the background text
* values.
*
* @return void
*/
protected function updateValue()
{
$name = $this->getName();
foreach ($this->getDataSources() as $ds) {
if (null !== ($value = $ds->getValue($name))
&& $value !== $this->btText
) {
$this->setValue($value);
return;
}
}
}
}
?>

View File

@ -0,0 +1,25 @@
<?php
require_once 'HTML/QuickForm2/Rule/Callback.php';
/**
* Custom rule that behaves like a callback but inverts the result.
*
* @author Christian Weiske <cweiske@php.net>
*/
class SemanticScuttle_QuickForm2_Rule_ICallback
extends HTML_QuickForm2_Rule_Callback
{
/**
* Validates the owner element.
* Inverts the return value of the callback.
*
* @return bool The value returned by a callback function
*/
protected function validateOwner()
{
return !parent::validateOwner();
}
}
?>

View File

@ -29,6 +29,17 @@ if (!$GLOBALS['enableRegistration']) {
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Renderer.php';
require_once 'SemanticScuttle/QuickForm2/Element/BackgroundText.php';
require_once 'SemanticScuttle/QuickForm2/Rule/ICallback.php';
HTML_QuickForm2_Factory::registerElement(
'backgroundtext',
'SemanticScuttle_QuickForm2_Element_BackgroundText'
);
HTML_QuickForm2_Factory::registerRule(
'icallback',
'SemanticScuttle_QuickForm2_Rule_ICallback'
);
$form = new HTML_QuickForm2(
'registration', 'post',
@ -36,7 +47,7 @@ $form = new HTML_QuickForm2(
true
);
$form->addElement(
$user = $form->addElement(
'text', 'username',
array(
'id' => 'username',
@ -44,31 +55,26 @@ $form->addElement(
'onkeyup' => 'isAvailable(this, "")',
'class' => 'required'
)
)
->setLabel(T_('Username'))
->addRule(
)->setLabel(T_('Username'));
$user->addRule(
'required',
T_('You <em>must</em> enter a username, password and e-mail address.')
)->and_(
$form->createRule(
'callback',
T_('This username is not valid (too short, too long, forbidden characters...), please make another choice.'),
array($userservice, 'isValidUsername')
)
)->and_(
$form->createRule(
'callback',
T_('This username already exists, please make another choice.'),
array($userservice, 'getUserByUsername')
)
)->and_(
$form->createRule(
'callback',
T_('This username has been reserved, please make another choice.'),
array($userservice, 'isReserved')
)
);
//FIXME: ajax-verification
$user->addRule(
'callback',
T_('This username is not valid (too short, too long, forbidden characters...), please make another choice.'),
array($userservice, 'isValidUsername')
);
$user->addRule(
'icallback',
T_('This username has been reserved, please make another choice.'),
array($userservice, 'isReserved')
);
$user->addRule(
'icallback',
T_('This username already exists, please make another choice.'),
array($userservice, 'existsUserWithUsername')
);
$form->addElement(
'password', 'password',
@ -84,39 +90,37 @@ $form->addElement(
T_('You <em>must</em> enter a username, password and e-mail address.')
);
$form->addElement(
$email = $form->addElement(
'text', 'email',
array(
'id' => 'email',
'size' => 40,
'class' => 'required'
)
)
->setLabel(T_('E-mail'))
->addRule(
)->setLabel(T_('E-mail'));
$email->addRule(
'required',
T_('You <em>must</em> enter a username, password and e-mail address.')
)->and_(
$form->createRule(
'callback',
T_('E-mail address is not valid. Please try again.'),
array($userservice, 'isValidEmail')
)
);
$email->addRule(
'callback',
T_('E-mail address is not valid. Please try again.'),
array($userservice, 'isValidEmail')
);
$form->addElement(
'text', 'antispamAnswer',
'backgroundtext', 'antispamAnswer',
array(
'id' => 'antispamAnswer',
'size' => 40
)
//FIXME: set antispam question text into value
// and automatically remove it (blur/focus)
)
->setLabel(T_('Antispam question'))
->setBackgroundText($GLOBALS['antispamQuestion'])
->setBackgroundClass('deact')
->addRule(
'callback',
T_('E-mail address is not valid. Please try again.'),
T_('Antispam answer is not valid. Please try again.'),
'verifyAntiSpamAnswer'
);
//FIXME: custom rule or captcha element
@ -131,7 +135,7 @@ function verifyAntiSpamAnswer($userAnswer)
return strcasecmp(
str_replace(' ', '', $userAnswer),
str_replace(' ', '', $GLOBALS['antispamAnswer'])
) != 0;
) == 0;
}
$tplVars['error'] = '';
@ -162,8 +166,7 @@ $renderer->setOption(
)
);
$tplVars['form'] = $form->render($renderer);
//var_dump($tplVars['form']);
$tplVars['form'] = $form->render($renderer);
$tplVars['loadjs'] = true;
$tplVars['subtitle'] = T_('Register');
$tplVars['error'] .= implode(