How to use form validation rules (Form Validation Rules) in the CodeIgniter framework

王林
Release: 2023-07-28 17:18:02
Original
661 people have browsed it

How to use form validation rules (Form Validation Rules) in the CodeIgniter framework

CodeIgniter is a popular PHP framework that provides many convenient features for developing web applications. One of the important features is form validation. Form validation allows developers to validate submitted form data and ensure data accuracy and completeness. This article will introduce how to use form validation rules in the CodeIgniter framework.

The first step is to ensure that you have correctly installed and configured the CodeIgniter framework. After this, you need to create a controller to handle the form request and related validation rules. The following is a simple sample code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class FormValidationController extends CI_Controller {

    public function index() {
        // 加载表单验证库
        $this->load->library('form_validation');

        // 设置验证规则
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');

        if ($this->form_validation->run() == FALSE) {
            // 表单验证失败,重新显示表单并显示错误消息
            $this->load->view('form_view');
        } else {
            // 表单验证成功,继续处理其他逻辑
            $this->load->view('success_view');
        }
    }
}
Copy after login

The above code first loads CodeIgniter's form validation library, and then sets two validation rules. The first rule specifies that the username field is required, and the second rule specifies that the password field is required and must be at least 6 characters long. You can customize and combine different validation rules according to your needs.

When the run() method is called, the form validation library will check and verify the submitted form data in turn. If validation fails, the controller will reload the form view and display an error message. If the validation is successful, the controller loads the success view and continues processing other logic.

Next, you need to create a view file to display the form and receive user input. The following is a simple sample code:

<!DOCTYPE html>
<html>
<head>
    <title>Form Validation Example</title>
</head>
<body>

    <?php echo validation_errors(); ?>

    <?php echo form_open('formvalidationcontroller'); ?>

    <label for="username">Username</label>
    <input type="text" name="username" /><br />

    <label for="password">Password</label>
    <input type="password" name="password" /><br />

    <input type="submit" value="Submit" />

    <?php echo form_close(); ?>

</body>
</html>
Copy after login

The above code uses the form_open method provided by CodeIgniter to create a form that contains the address of the form validation controller. Error messages can be displayed above the form by calling the validation_errors method. You can customize the form style and structure according to your needs.

Finally, when we enter the username and password and submit the form, the form validation controller will process and load the corresponding view based on the validation results. The following is a code example of a simple success page:

<!DOCTYPE html>
<html>
<head>
    <title>Success Page</title>
</head>
<body>
    <h1>Success!</h1>
    <p>Your form has been successfully submitted.</p>
</body>
</html>
Copy after login

Through the above steps, we can use form validation rules in the CodeIgniter framework to ensure the accuracy and completeness of submitted form data. This not only improves the security of the application but also enhances the user experience.

I hope this article will help you understand how to use form validation rules to perform form validation in the CodeIgniter framework.

The above is the detailed content of How to use form validation rules (Form Validation Rules) in the CodeIgniter framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!