file form validation codeigniter

// Load CodeIgniter Form Validation Library
$this->load->library('form_validation');

// Set validation rules for the form fields
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');

// Set custom error messages for validation rules
$this->form_validation->set_message('required', '{field} is required');
$this->form_validation->set_message('min_length', '{field} must be at least {param} characters');
$this->form_validation->set_message('max_length', '{field} cannot exceed {param} characters');
$this->form_validation->set_message('valid_email', 'Please enter a valid email address');
$this->form_validation->set_message('matches', 'The {field} field must match the {param} field');

// Run the form validation
if ($this->form_validation->run() == FALSE) {
    // Validation failed, load the form view with validation errors
    $this->load->view('form_view');
} else {
    // Validation passed, process the form data
    $username = $this->input->post('username');
    $email = $this->input->post('email');
    $password = $this->input->post('password');

    // Additional processing or database operations can be performed here

    // Redirect to a success page or load a success view
    $this->load->view('success_view');
}