Registration Page Code Listing
From DragonPHP
NOTE: The following files must be created over your default Starter Web Application 0.1.0 in order to begin the Database Access Tutorial.
Contents |
[edit] Controllers
[edit] BASE_APPLICATION_DIR/modules/Default/controllers/Registration.php
<?php
require_once(DRAGON_CONTROLLER);
class Registration extends Controller {
public function execute(Request $request, Session $session, $view) {
// Check if there was a form submission?
if(!$request->getParameter(SUBMIT_PARAM)){
// set unique token to prevent double submission
$this->setFormToken();
// render the registration form
return new Template('registration');
}else{
if($this->isValidSubmission()){
// grab data from the form
$userId = $request->getParameter('user_id');
$emailAddress = $request->getParameter('email_address');
// set attributes for the template ( the view )
$this->setAttribute('user_id', $userId);
$this->setAttribute('email_address', $emailAddress);
// reset token to prevent double form submission
$this->resetFormToken();
}
// render thank you page.
return new Template('thank_you');
}
}
}
?>
[edit] BASE_APPLICATION_DIR/modules/Default/controllers/RegistrationValidator.php
<?php
require_once(VALIDATOR);
class RegistrationValidator extends RequestValidator {
const USER_ID = 'user_id';
const EMAIL_ADDRESS = 'email_address';
// This must be set because it determines which template is
// used in case an error is thrown
protected $_errorTemplateSetId = 'registration';
public function validate(Request $request, Session $session) {
// set validation paramters
self::isRequired(self::USER_ID);
self::isRequired(self::EMAIL_ADDRESS);
// validate email address
$emailAddress = $request->getParameter(self::EMAIL_ADDRESS);
$emailPattern = '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$';
if (isset($emailAddress)){
if(ereg($emailPattern, $emailAddress) != 1) {
// found a validation error
$this->_setError(self::EMAIL_ADDRESS, 'Invalid email address');
}
}
// validate length of user id
$userId = $request->getParameter(self::USER_ID);
if (isset($userId)){
if(strlen($userId) > 10){
$this->_setError(self::USER_ID, 'User Id can not exceed 10 characters');
}
}
}
}
?>
[edit] Template Files
[edit] BASE_APPLICATION_DIR/modules/Default/templates/registration.tpl
<html>
<body>
{$template_tile->header}
{$template_tile->registration_form}
{$template_tile->footer}
</body>
</html>
[edit] BASE_APPLICATION_DIR/modules/Default/templates/registration_form.tpl
<!-- display error message -->
{if isset($model->errors)}
<p>Error
{foreach from=$model->errors key=key item=item}
<li>{$item}</li>
{/foreach}
</p>
{/if}
<form action="index.php" method="POST">
<input type="hidden" name="module" value="{$model->module}">
<input type="hidden" name="controller" value="{$model->controller}">
<p>user id: <input type="text" name="user_id" value="{$model->user_id}"></p>
<p>email address: <input type="text" name="email_address" value="{$model->email_address}"></p>
<input type="submit" name="submit" class="submit_button" value="Register">
</form>
[edit] BASE_APPLICATION_DIR/modules/Default/templates/thank_you.tpl
<html>
<body>
{$template_tile->header}
<br/>
Thank You!
<br/>
<span style="font-weight:bold">user id </span>'{$user_id}' was registered to
<span style="font-weight:bold">email address </span>'{$email_address}'!
{$template_tile->footer}
</body>
</html>
[edit] Config Files
[edit] BASE_APPLICATION_DIR/modules/Default/conf/layout_definitions.ini
[default] header=global main_body=local footer=global [registration] header=global registration_form=local footer=global [thank_you] header=global footer=global
[edit] BASE_APPLICATION_DIR/conf/main.php
<?php
// ### IMPORTANT: YOU MUST DEFINE THESE DEFINITIONS ###
define('BASE_DIR', '/Path/To/Your/Workspace/');
define('APPLICATION_NAME', 'tutorial');
define('FRAMEWORK_DIR', '/Path/To/The/Dragon/Framework/');
define('CACHE_DIR', BASE_DIR . '/Path/To/Cache/Dir/(chmod 755)/(chmod nobody nobody)/');
// #########################################
//
// ****************** README *******************************
// ** This section, above, with the paths can cause a lot of headaches
// ** if you don't set it up correctly
// ** Below is an example of a working setup. (Ben Mullin 9/12/07)
// define('BASE_DIR', '/Users/bmullin/Workspace/php/');
// define('APPLICATION_NAME', 'web_application');
// define('FRAMEWORK_DIR', '/Users/bmullin/Workspace/php/dragonphp_framework_7.0.5/');
// define('CACHE_DIR', '/Users/bmullin/Workspace/php/cache/');
//
// ***********************************************************
//
define('BASE_APPLICATION_DIR', BASE_DIR . APPLICATION_NAME . '/');
define('LOG_DIR', CACHE_DIR . APPLICATION_NAME . '/logs/');
/* Set Logger Level
0 = DEBUG
1 = INFO
2 = WARNING
3 = ERROR
*/
define('LOGGER_LEVEL', 0);
// Define logger date format
define('LOGGER_DATE_FORMAT', 'm/d/Y H:m:s');
// IMPORTANT: Define a default module and controller (page and view)
define('DEFAULT_MODULE', 'Default');
define('DEFAULT_CONTROLLER', 'Index');
define('DEFAULT_VIEW_CONTROLLER', 'Index');
define('DEFAULT_TILE_SECTION', 'default');
define('DEFAULT_HEADER', 'header.tpl');
define('DEFAULT_FOOTER', 'footer.tpl');
// Default view renderer
define('DEFAULT_VIEW_RENDERER', 'SmartyRenderer');
// include the framework's definitions
include_once('framework.php');
// Define Active Record plugin
define('ACTIVE_RECORD', ACTIVE_RECORD_MYSQL_PDO);
?>
[edit] BASE_APPLICATION_DIR/conf/url_mapper.ini
[shortcuts] default="module=Default&controller=Index.php" main="module=Default&controller=Main" login="module=Security&controller=Login" registration="module=Default&controller=Registration"
