Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Controller User
- public function action_create() {
- $view = View::factory('user/create');
- $view->errors = array();
- if (HTTP_Request::POST == $this->request->method()) {
- try {
- //Add additional values
- $additional = array(
- 'ip' => $_SERVER['REMOTE_ADDR'],
- 'register_date' => time(),
- 'activation_key' => sha1(uniqid(Text::random('alnum', 32), TRUE))
- );
- $post = Arr::merge($this->request->post(), $additional);
- // Create the user using form values
- $user = ORM::factory('user');
- $user->create_user($post, array(
- 'username',
- 'password',
- 'email',
- 'register_date',
- 'activation_key',
- 'ip'
- ));
- // Grant user login role
- $user->add('roles', ORM::factory('role', array('name' => 'login')));
- // Reset values so form is not sticky
- $_POST = array();
- } catch (ORM_Validation_Exception $e) {
- $view->errors = $e->errors(I18n::$lang);
- }
- }
- $this->content = $view->render();
- }
- //Model
- defined('SYSPATH') or die('No direct access allowed.');
- class Model_User extends Model_Auth_User {
- //Need Columns for PDO
- protected $_table_columns = array(
- 'id' => array(),
- 'email' => array(),
- 'username' => array(),
- 'password' => array(),
- 'ip' => array(),
- 'register_date' => array(),
- 'activation_key' => array(),
- 'logins' => array(),
- 'last_login' => array()
- );
- public function rules() {
- return array(
- 'username' => array(
- array('not_empty'),
- array('alpha_dash'),
- array('min_length', array(':value', 4)),
- array('max_length', array(':value', 32)),
- array(array($this, 'unique'), array('username', ':value')),
- ),
- 'password' => array(
- array('not_empty'),
- ),
- 'email' => array(
- array('not_empty'),
- array('email'),
- array(array($this, 'unique'), array('email', ':value')),
- ),
- 'terms' => array(
- array('equals', array(':value', 'on')), //Added Terms
- ),
- );
- }
- public function extra_rules() {
- return array(); //extra rules are empty now
- return array(
- 'terms' => array(
- array('equals', array(':value', 'on')),
- ),
- );
- }
- public static function get_password_validation($values) {
- return Validation::factory($values)
- ->rule('password', 'min_length', array(':value', 6))
- ->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
- }
- public function create_user($values, $expected) {
- // Validation for passwords
- $extra_validation = Model_User::get_password_validation($values)
- ->rule('password', 'not_empty');
- //Extend with extra validation
- foreach ($this->extra_rules() as $field => $rules) {
- $extra_validation->rules($field, $rules);
- }
- return $this->values($values, $expected)->create($extra_validation);
- }
- public function update_user($values, $expected = NULL) {
- if (empty($values['password'])) {
- unset($values['password'], $values['password_confirm']);
- }
- // Validation for passwords
- $extra_validation = Model_User::get_password_validation($values);
- //Extend with extra validation
- foreach ($this->extra_rules() as $field => $rules) {
- $extra_validation->rules($field, $rules);
- }
- return $this->values($values, $expected)->update($extra_validation);
- }
- }
- // End User Model
- //View Create
- <div class="title"><?= __('Registration') ?></div>
- <div class="register">
- <?php
- $username = Arr::path($errors, 'username');
- $email = Arr::path($errors, 'email');
- $password = Arr::path($errors, '_external.password');
- $password_confirm = Arr::path($errors, '_external.password_confirm');
- $terms = Arr::path($errors, 'terms');
- echo Debug::vars($errors); //no terms errors displayer
- ?>
- <ul class="error">
- <?php if ($username != NULL)
- ?>
- <li><?= $username ?></li>
- <?php if ($email != NULL)
- ?>
- <li><?= $email ?></li>
- <?php if ($password != NULL)
- ?>
- <li><?= $password ?></li>
- <?php if ($password_confirm != NULL)
- ?>
- <li><?= $password_confirm ?></li>
- <?php if ($terms != NULL)
- ?>
- <li><?= $terms ?></li>
- </ul>
- <?= Form::open(I18n::$lang . '/user/create') ?>
- <?= Form::label('username', __('Username') . ' :') ?>
- <?= Form::input('username', HTML::chars(Arr::get($_POST, 'username'))) ?>
- <?= Form::label('password', __('Password') . ' :') ?>
- <?= Form::password('password', HTML::chars(Arr::get($_POST, 'password'))) ?>
- <?= Form::label('password_confirm', __('Password Confirm') . ' :') ?>
- <?= Form::password('password_confirm', HTML::chars(Arr::get($_POST, 'password_confirm'))) ?>
- <?= Form::label('email', __('E-Mail') . ' :') ?>
- <?= Form::input('email', HTML::chars(Arr::get($_POST, 'email'))) ?>
- <?= Form::label('terms', HTML::anchor(I18n::$lang . '/page/terms_an_conditions', __('Accept terms and conditions') . '?')) ?>
- <?= Form::hidden('terms', 'off') ?>
- <?= Form::checkbox('terms') ?>
- <?= Form::submit('register', __('Register'), array('class' => 'window button registerButton')) ?>
- <?= Form::close() ?>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement