• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

CodeIgniter 9- Form validation trong Codeigniter

tiendat3791

New Member
Bài viết trước Bài 8. Database trong Codeigniter chúng ta đã tìm hiểu về library đầu tiên, bài viết này hãy cùng Công ty thiết kế website OTVINA tìm hiểu về Form Validation 1 thư viện giúp kiểm tra tính hợp lệ của dữ liệu trong form.
[h=2]Load thư viện Form validation[/h]Như tìm hiểu ở bài trước trước khi sử dụng chúng ta phải load thư viện ra bằng cách:
$this->load->library('form_validation');[h=2]Một số phương thức trong thư viện Form validation[/h][h=3]Tạo luật với set_rules()[/h]Set_rules là phương thức được xây dựng để tạo 1 tập luật cho dữ liệu từ form.
<?php
$this->form_validation->set_rules('name_cua_input','Tên nhãn khi xuất thông báo lỗi','tập luật');
?>Ví dụ: Yêu cầu bắt buộc nhập trường họ tên với HTML
<form action="" method="post">
<input type="text" name="hoten" />
</form>Để bắt buộc nhập trường này ta làm như sau:
<?php
$this->form_validation->set_rules('hoten','Họ tên','required');
?>Trong trường hợp nhiều luật áp dụng chúng ta sẽ ngăn cách bằng dấu |
<?php
$this->form_validation->set_rules('hoten','Họ tên','required|luat2|luat3');
?>Một số tập luật trong thư viện

  • required: Bắt buộc nhập
  • is_unique[tenbang.tentruong]: Kiểm tra trường là duy nhất
  • regex_match: Thêm tập luật bằng Regular Expression
  • matches: Kiểm tra tính hợp nhất của dữ liệu. Ví dụ kiểm tra password nhập lại phải giống giá trị password: matches[password]
  • min_length: Giá trị độ dài kí tự tối thiểu nhập vào. VD: min_length[6]
  • max_length: Giá trị độ dài kí tự tối đa nhập vào.
  • numberic: Dữ liệu phải là số
  • valid_email: Kiểm tra tính hợp lệ của email
  • valid_url: Kiểm tra tính hợp lệ đường dẫn
  • xss_clean: Xóa Xss của input để bảo mật, khi sử dụng luật này bạn lưu ý phải gọi helper security.
  • exact_length: Kiểm tra độ dài chính xác: exact_length[10]
  • greater_than: Kiểm tra độ dài phải lớn hơn độ dài được gán giá trị: greater_than[10]
  • less_than: Kiểm tra độ dài phải nhỏ hơn độ dài được gán giá trị: less_than[10]
  • callback_tenfunction: Gọi 1 hàm xử lý khác nếu dữ liệu cần xử lý nhiều luật hơn
Ngoài ra bạn có thể tìm hiểu thêm các tập luật khác tại: Form Validation CodeIgniter 3.1.2 documentation
[h=3]Phương thức run()[/h]Phương thức run() có chức năng chạy kiểm tra tập lệnh đã set ở phương thức set_rules, run() trả về true nếu tất cả các dữ liệu hợp lệ.
[h=3]Phương thức form_error()[/h]Là phương thức trả về thông báo lỗi sau khi thiết lập tập lệnh.
Cú pháp:
<?php
form_error('ten_input');
?>

[h=3]Phương thức set_value();[/h]Là phương thức lấy lại giá trị cũ đã nhập nếu form bị lỗi do không hợp lệ với tập luật, có nghĩa là nếu bạn submit form mà dữ liệu không hợp lệ thì dữ liệu cũ sẽ bị xóa đi,nhưng nếu bạn sử dụng set_value() thì dữ liệu cũ sẽ được lưu lại nếu submit dữ liệu không thành công.
<?php
set_value('ten_input');
?>[h=2]Ví dụ về Form validation cho form đăng ký thành viên đơn giản[/h]1. Tạo Controller: Tạo controller user.php
<?php
class User extends CI_Controller{
public function __construct(){
$this->load->library('form_validation');
}
public function register(){

//Bắt đầu set tập lệnh
$this->form_validation->set_rules('username','Tài khoản','required|min_length[6]'');
$this->form_validation->set_rules('password','Mật khẩu','required|min_length[6]'');
$this->form_validation->set_rules('repassword','Nhập lại mật khẩu','required|min_length[6]|matches[password]'');

//Bắt đầu chạy kiểm tra tập lệnh
if($this->form_validation->run()){
//Đăng ký thành công, muốn làm gì thêm thì làm :D
}
$this->load->view('themes/register');
}
}
?>2. View: themes/register.php
<form action="user/register" method="post">
<input type="text" value="<?php echo set_value('username');?>" name="username" placeholder="Tài khoản" />
<p class="error"><?php echo form_error('username');?></p>
<input type="password" value="<?php echo set_value('password');?>" name="password" />
<p class="error"><?php echo form_error('password');?></p>
<input type="password" value="<?php echo set_value('repassword');?>" name="repassword" />
<p class="error"><?php echo form_error('repassword');?></p>
</form>Khi submit dữ liệu xảy ra lỗi do không hợp lệ với tập lệnh đã set thì dữ liệu cũ do người dùng nhập sẽ được lưu lại do dùng phương thức set_value, thông báo lỗi sẽ xuất hiện dưới trường dữ liệu với phương thức form_error();
Vậy là bài này chúng ta đã xong phần khá quan trọng và đa số gặp khi thiết kế website đó là Form Validation giúp dữ liệu được rõ ràng và bảo mật hơn. Hẹn gặp các bạn trong những bài tiếp theo <3

Nguồn : CodeIgniter 9- Form validation trong Codeigniter
 

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
T CodeIgniter 16- Quản lý cookie trong CI Mã nguồn web 0
T CodeIgniter 7- Tìm hiểu Helpers và Libraries là gì? Mã nguồn web 0
T CodeIgniter 15- Thực hành đăng nhập/đăng xuất Mã nguồn web 0
T CodeIgniter 4- Controller trong CodeIgniter Mã nguồn web 0
T CodeIgniter 18- Helper language, đa ngôn ngữ trong CI Mã nguồn web 0
T CodeIgniter 10- Thư viện upload file PHP 1
T CodeIgniter 2 - Hướng dẫn cài đặt CodeIgniter PHP 2
T Codeigniter 19- Helper date trong Codeigniter Tin tức CNTT 1
Admin Share code tin tức bằng codeigniter Mã nguồn web 0
Admin Giữ nguyên giá trị khi gửi form php - keep form values after post php PHP 0
Admin Giữ giá trị trong menu đổ xuống trong form php - Php keep dropdown value after form submit PHP 0
Admin [OzzModz] Remove Register Button From Login Form Xenforo 0
Admin Hướng dẫn thêm form liên hệ (contact) cho wordpress mới nhất Wordpress 0
Admin Hướng dẫn hiển thị thông báo nhắc nhở khi đóng form C# C# / C++ 0
T Thiết kế áo khoác dạ nữ sài gòn tphcm cổ tròn đẹp form dáng hàn quốc thu đông 2018 – 2019 Trò chuyện linh tinh 0
Admin GUI Design Studio Full Key mới nhất - Phần mềm thiết kế giao diện Windows Form hàng đầu Phần mềm 1
Admin Share form login đẹp cho vbb Style vbb 0
Admin Share template trang liên hệ hình bìa thư rất đẹp - modal contact form template HTML & CSS 0
Admin Bbcode hiển thị form google docs Vbulletin 0
N Share Sleek & Modern Login Form All Shared Scripts 0
Admin [HTML] Form - Biểu mẫu trong HTML HTML & CSS 1
Admin Form giải toán phương trình bậc 2 Javascript/ajax 0
Admin Form "chôm" avatar của yahoo nhập vào Javascript/ajax 0
M form lại open lại admin share vài domain đi Trò chuyện linh tinh 5
C SPYNXSERVICE AMAZON VALIDATION EMAILS Tut, tool, mmo 0
N SRC-Email Validation Checker Source Code C# / C++ 0

Similar threads

New posts New threads New resources

Back
Top