'CodeIgniter getting form field value to lowercase
I have just started on learning how to use CodeIgniter and have never use any framework before so I only know a bit of how is the flow. Right now I have 1 issue that I want to set the input username to lowercase and I do not have any idea how to write the function for the convert_lowercase().
Below is my code:
public function signup_validation()
{
$this ->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|convert_lowercase');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_message('is_unique', 'That Username Already Exists.');
if($this->form_validation->run()){
}else{
$this->load->view('signup');
}
}
public function convert_lowercase()
{
strtolower($this->input->post('username'));
}
I am not sure am I doing the right way.
And is it best to just put the strtolower in the set_rules parameter? Or it is best to put in a function?
And if separate it, how should it be done and how do I get the final username data to insert into database?
Any kind souls out there can help me on this?
Thanks in advance.
Solution 1:[1]
You can provide php native functions for form validation to CodeIgniter. Here is how your code should be
public function signup_validation()
{
$this ->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|strtolower');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_message('is_unique', 'That Username Already Exists.');
if($this->form_validation->run()){
}else{
$this->load->view('signup');
}
}
You should check their documentation on form validation: http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
Solution 2:[2]
add callback_ to the rules.
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|callback_convert_lowercase');
and the callback function should return some value.
public function convert_lowercase() {
return strtolower($this->input->post('username'));
}
Solution 3:[3]
I will try to explain the best I can! Have you set up your database config file correctly? Have you set up the database correctly? Make sure that is all good before you do this..
Here is a bit of what is going on
if($this->form_validation->run()){
//Right here is what happens if the form passes your test!
$this->insert_user();
}else{
$this->load->view('signup');
}
if($this->form_validation->run()) takes the rules you gave it and if it returns "true" it runs in that if statement otherwise it will return you to the signup page
Here is the function that I set an example for
public function insert_user()
{
$data = array(
'username' => strtolower($this->input->post('username')),
'password' => $this->input->post('password'),
);
$this->db->insert('users', $data);
}
I also suggest you look into encrypting your passwords and other CI documentation, it is fantastic
Solution 4:[4]
Regarding Codeigniter 4 it is no longer possible to modify data during validation.
They add this information in official documentation : "You can also use any native PHP functions that return boolean and permit at least one parameter, the field data to validate. The Validation library never alters the data to validate."
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bilal Budhani |
| Solution 2 | bprayudha |
| Solution 3 | Chitowns24 |
| Solution 4 | AradoN0 |
