first work on quickform2 registration form

This commit is contained in:
Christian Weiske 2010-05-03 22:55:53 +02:00
parent 31570df64c
commit 087189a503
3 changed files with 251 additions and 13 deletions

View File

@ -10,35 +10,36 @@ window.onload = function() {
<p><?php echo sprintf(T_('Sign up here to create a free %s account. All the information requested below is required'), $GLOBALS['sitename']); ?>.</p>
<form action="<?php echo $formaction; ?>" method="post">
<form<?php echo $form['attributes']; ?>>
<?php echo implode('', $form['hidden']); ?>
<table>
<tr>
<th align="left"><label for="username"><?php echo T_('Username'); ?></label></th>
<td><input type="text" id="username" name="username" size="20" class="required" onkeyup="isAvailable(this, '')" /> </td>
<th align="left"><?php echo $form['username']['labelhtml']; ?></th>
<td><?php echo $form['username']['html']; ?></td>
<td id="availability"><?php echo '←'.T_(' at least 5 characters, alphanumeric (no spaces, no dots or other special ones)') ?></td>
</tr>
<tr>
<th align="left"><label for="password"><?php echo T_('Password'); ?></label></th>
<td><input type="password" id="password" name="password" size="20" class="required" /></td>
<th align="left"><?php echo $form['password']['labelhtml']; ?></th>
<td><?php echo $form['password']['html']; ?></td>
<td></td>
</tr>
<tr>
<th align="left"><label for="email"><?php echo T_('E-mail'); ?></label></th>
<td><input type="text" id="email" name="email" size="40" class="required" /></td>
<th align="left"><?php echo $form['email']['labelhtml']; ?></th>
<td><?php echo $form['email']['html']; ?></td>
<td><?php echo '←'.T_(' to send you your password if you forget it')?></td>
</tr>
<?php if(strlen($antispamQuestion)>0) {?>
<?php if (isset($form['antispamAnswer'])) {?>
<tr>
<th align="left"><label for="antispamAnswer"><?php echo T_('Antispam question'); ?></label></th>
<td><input type="text" id="antispamAnswer" name="antispamAnswer" size="40" class="required" value="<?php echo $antispamQuestion; ?>" onfocus="if (this.value == '<?php echo $antispamQuestion; ?>') this.value = '';" onblur="if (this.value == '') this.value = '<?php echo $antispamQuestion; ?>';"/></td>
<th align="left"><?php echo $form['antispamAnswer']['labelhtml']; ?></label></th>
<td><?php echo $form['antispamAnswer']['html']; ?></td>
<td></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td><input type="submit" name="submitted" value="<?php echo T_('Register'); ?>" /></td>
<td><?php echo $form['submit']['html']; ?></td>
<td></td>
</tr>
</table>

View File

@ -0,0 +1,89 @@
<?php
require_once 'HTML/QuickForm2/Renderer/Array.php';
/**
* Custom HTML_QuickForm2 renderer allowing easy access
* to elements by their ID
*
* FIXME
*/
class SemanticScuttle_QuickForm2_Renderer_CoolArray
extends HTML_QuickForm2_Renderer_Array
implements ArrayAccess
{
protected $ids = array();
public function __construct()
{
parent::__construct();
}
/**
* Overwrite parent method to create ID index
*/
public function pushScalar(array $element)
{
parent::pushScalar($element);
$id = $element['id'];
$cont =& $this->containers[
count($this->containers) - 1
];
$this->ids[$id] =& $cont[count($cont) - 1];
}
/**
* Creates an array with fields that are common to all elements.
* This method here also creates html labels.
*
* @param HTML_QuickForm2_Node $element Element being rendered
*
* @return array Array of attributes
*/
public function buildCommonFields(HTML_QuickForm2_Node $element)
{
$ary = parent::buildCommonFields($element);
if (isset($ary['label'])) {
//FIXME: error class
//FIXME: htmlspecialchars()?
$ary['labelhtml'] = '<label for="' . $ary['id'] . '">'
. $ary['label'] . '</label>';
}
return $ary;
}
public function offsetSet($offset, $value)
{
$this->ids[$offset] = $value;
}
public function offsetExists($offset)
{
if (isset($this->array[$offset])) {
return true;
}
return isset($this->ids[$offset]);
}
public function offsetUnset($offset)
{
unset($this->ids[$offset]);
}
public function offsetGet($offset)
{
if (isset($this->array[$offset])) {
return $this->array[$offset];
}
return isset($this->ids[$offset])
? $this->ids[$offset]
: null;
}
}
?>

View File

@ -27,8 +27,156 @@ if (!$GLOBALS['enableRegistration']) {
exit(1);
}
/* Service creation: only useful services are created */
// No specific services
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Renderer.php';
$form = new HTML_QuickForm2(
'registration', 'post',
array('action' => createURL('register')),
true
);
$form->addElement(
'text', 'username',
array(
'id' => 'username',
'size' => 20,
'onkeyup' => 'isAvailable(this, "")',
'class' => 'required'
)
)
->setLabel(T_('Username'))
->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
$form->addElement(
'password', 'password',
array(
'id' => 'password',
'size' => 20,
'class' => 'required'
)
)
->setLabel(T_('Password'))
->addRule(
'required',
T_('You <em>must</em> enter a username, password and e-mail address.')
);
$form->addElement(
'text', 'email',
array(
'id' => 'email',
'size' => 40,
'class' => 'required'
)
)
->setLabel(T_('E-mail'))
->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')
)
);
$form->addElement(
'text', 'antispamAnswer',
array(
'id' => 'antispamAnswer',
'size' => 40
)
//FIXME: set antispam question text into value
// and automatically remove it (blur/focus)
)
->setLabel(T_('Antispam question'))
->addRule(
'callback',
T_('E-mail address is not valid. Please try again.'),
'verifyAntiSpamAnswer'
);
//FIXME: custom rule or captcha element
$form->addElement(
'submit', 'submitted', array('id' => 'submit')
)
->setLabel(T_('Register'));
function verifyAntiSpamAnswer($userAnswer)
{
return strcasecmp(
str_replace(' ', '', $userAnswer),
str_replace(' ', '', $GLOBALS['antispamAnswer'])
) != 0;
}
$tplVars['error'] = '';
if ($form->validate()) {
$arValues = $form->getValue();
//FIXME: how to fetch single values?
$bOk = $userservice->addUser(
$arValues['username'], $arValues['password'], $arValues['email']
);
if ($bOk) {
header('Location: '. createURL('bookmarks', $arValues['username']));
exit();
}
$tplVars['error'] .= T_('Registration failed. Please try again.');
}
HTML_QuickForm2_Renderer::register(
'coolarray',
'SemanticScuttle_QuickForm2_Renderer_CoolArray'
);
require_once 'SemanticScuttle/QuickForm2/Renderer/CoolArray.php';
//$renderer = HTML_QuickForm2_Renderer::factory('coolarray')
$renderer = new SemanticScuttle_QuickForm2_Renderer_CoolArray();
$renderer->setOption(
array(
'group_hiddens' => true,
'group_errors' => true
)
);
$tplVars['form'] = $form->render($renderer);
//var_dump($tplVars['form']);
$tplVars['loadjs'] = true;
$tplVars['subtitle'] = T_('Register');
$tplVars['error'] .= implode(
'<br/>', array_unique($tplVars['form']['errors'])
);
$templateservice->loadTemplate('register.tpl', $tplVars);
exit();
/* Managing all possible inputs */
isset($_POST['submitted']) ? define('POST_SUBMITTED', $_POST['submitted']): define('POST_SUBMITTED', '');