public function testUserFormHandler() { /** Assertions: * Existing email passes validation * Unset email fails validation * Matching passwords pass validation * Mismatched passwords fail validation */ $form = new Form_Handler_User(false); $input = array('username' => 'test', 'email' => '*****@*****.**'); $result = $form->validate($input); $this->assertTrue($result, "Validation of existing email requirement failed: " . $this->getErrorMessages($form)); $input = array('username' => 'test'); $result = $form->validate($input); $this->assertFalse($result, "Validation of unset email requirement failed"); $form = new Form_Handler_User(true); $input = array('email' => '*****@*****.**', 'password' => 'testpass', 'password2' => 'testpass'); $result = $form->validate($input); $this->assertTrue($result, "Validation of matching passwords failed: " . $this->getErrorMessages($form)); $input = array('email' => '*****@*****.**', 'password' => 'testpass', 'password2' => 'notright'); $result = $form->validate($input); $this->assertFalse($result, "Validation of mismatched passwords failed"); }
<?php $form = new Form_Handler_User(!$user->exists() || !empty($_POST['password']) ? true : false); $form->validate(); $errors = $form->errors(); if ($errors) { $pm->setVariable('errors', $errors); $pm->setVariable('user', $form->input()); } else { $user->setArray($_POST, false); $user['password'] = $_POST['password']; $user->save(); if (defined('TYPEF_HOST')) { $sites = Model_User_Site::ForUserId($user['userid']); $sites->deleteQuery(); if (!empty($_POST['admin_siteid'])) { foreach ($_POST['admin_siteid'] as $siteid) { $site = Model_User_Site::Create(); $site['userid'] = $user['userid']; $site['siteid'] = $siteid; $site->save(); } } } Typeframe::Redirect('User saved.', Typeframe::CurrentPage()->applicationUri()); }
/** * User account register/index controller. * * Allows users to register. Sends emails in the event of confirmation or approval being required. * * @package User */ // if registration is disabled, then we're done here if (TYPEF_ALLOW_REGISTRATION == 0) { Typeframe::Redirect('Registration has been disabled.', TYPEF_WEB_DIR . '/', -1); return; } // create user and a formhandler for them $user = Model_User::Create(); $form = new Form_Handler_User($user); // process the form if ('POST' == $_SERVER['REQUEST_METHOD']) { // validate the form; get errors $form->validate(); $errors = $form->errors(); if (count($errors)) { // add errors to template $pm->setVariable('errors', $errors); } else { $user->set('username', $_POST['username']); $user->set('password', $_POST['password']); $user->set('email', $_POST['email']); $user->set('usergroupid', TYPEF_DEFAULT_USERGROUPID); $user->set('firstname', $_POST['firstname']); $user->set('lastname', $_POST['lastname']);