CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL auto_increment, `email` varchar(254) NOT NULL, `username` varchar(32) NOT NULL default '', `password` varchar(64) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_username` (`username`), UNIQUE KEY `uniq_email` (`email`) );
2.สร้างไฟล์ application/views/user/register.php
<?php defined('SYSPATH') or die('No direct script access.'); ?>
<?php echo Form::open() ?>
<?php if ($errors): ?>
<p class="message">Some errors were encountered, please check the details you entered.</p>
<ul class="errors">
<?php foreach ($errors as $message): ?>
<li><?php echo $message ?></li>
<?php endforeach ?>
<?php endif ?>
<dl>
<dt><?php echo Form::label('username', 'Username') ?></dt>
<dd><?php echo Form::input('username', $post['username']) ?></dd>
<dt><?php echo Form::label('password', 'Password') ?></dt>
<dd><?php echo Form::password('password') ?></dd>
<dd class="help">Passwords must be at least 6 characters long.</dd>
<dt><?php echo Form::label('confirm', 'Confirm Password') ?></dt>
<dd><?php echo Form::password('confirm') ?></dd>
<dt><?php echo Form::label('email', 'Email') ?></dt>
<dd><?php echo Form::input('email', $post['email']) ?></dd>
</dl>
<?php echo Form::submit(NULL, 'Sign Up') ?>
<?php echo Form::close() ?>
3.สร้างไฟล์ application/classes/controller/user.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_User extends Controller_Template {
public $template = 'template';
// nothing here
public function action_index()
{
$this->request->redirect('');
}
public function action_register()
{
$user = ORM::factory('user');
if (isset($_POST) && Valid::not_empty($_POST)) {
$post = Validation::factory($_POST)
->rule('username', 'not_empty')
->rule('username', 'regex', array(':value', '/^[a-z_.]++$/iD'))
->rule('username', array($user, 'unique_username'))
->rule('password', 'not_empty')
->rule('password', 'min_length', array(':value', 6))
->rule('confirm', 'matches', array(':validation', ':field', 'password'))
->rule('email', 'not_empty');
if ($post->check())
{
$user->values(array(
'email' => $post['email'],
'username' => HTML::entities(strip_tags($post['username'])),
'password' => $post['password'],
));
$user->save();
}
// Validation failed, collect the errors
$errors = $post->errors('user');
}
// Display the registration form
// display
$this->template->content = View::factory('user/register')
->bind('post', $post)
->bind('errors', $errors);
}
}
4.สร้างไฟล์ application/classes/model/user.php
<?php defined('SYSPATH') or die('No direct script access.');
class Model_User extends ORM {
public static function unique_username($username)
{
// Check if the username already exists in the database
return ! DB::select(array(DB::expr('COUNT(username)'), 'total'))
->from('users')
->where('username', '=', $username)
->execute()
->get('total');
}
}
5.สร้างไฟล์ application/messages/user.php
เป็นการกำหนดข้อความ Error เอง โดยไฟล์นี้ถูกเรียกใช้จาก application/classes/controller/user.php ในโค้ด $errors = $post->errors('user');
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'username' => array(
'unique_username' => 'This username already exists in the database'
)
);
หมายเหตุ ถ้าเปลี่ยน $errors = $post->errors('user/validation'); เราก็ต้องสร้างไฟล์ application/messages/user/validation.php แทนครับ
หัดเล่นอยู่ เลยเอามาลงที่นี้นะครับ
ตอบลบ