CodeIgniter gets lowercase form field value

I just started learning how to use CodeIgniter and never use the framework before, so I only know a little about how to stream. Right now I have 1 problem that I want to set the input username to lowercase and I don’t know how to write a function for 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'm not sure I'm doing the right way.

And is it better to just put strtolower in the set_rules parameter? Or is it best to enable the feature?

And if it's separate, how to do it and how do I get the final username data to insert into the database?

Any souls can help me with this?

Thanks in advance.

+3
source share
3 answers

php CodeIgniter.

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');
    }
}

: http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html

+5

, ! ? ? , , .

,

 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()) , , "true", if,

,

public function insert_user()
{

        $data = array(


            'username' => strtolower($this->input->post('username')),
            'password' => $this->input->post('password'),





        );

        $this->db->insert('users', $data);




}

CI,

+2

callback_ .

$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'));
}
+2
source

All Articles