【问题标题】:CodeIgniter getting form field value to lowercaseCodeIgniter 将表单字段值转换为小写
【发布时间】:2022-04-06 17:16:32
【问题描述】:

我刚刚开始学习如何使用 CodeIgniter,之前从未使用过任何框架,所以我只知道一点流程是怎样的。现在我有 1 个问题,我想将输入用户名设置为小写,但我不知道如何为 convert_lowercase() 编写函数。

下面是我的代码:

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

我不确定我的做法是否正确。

最好将 strtolower 放在 set_rules 参数中吗?还是最好放一个函数?

如果分开,应该怎么做,如何获取最终的用户名数据插入数据库?

有哪位好心人可以帮我解决这个问题?

提前致谢。

【问题讨论】:

  • 您是要 a) 禁止用户名中的大写字母,还是 b) 只是将其转换为小写?如果a)->也许这会有所帮助> php.net/manual/en/function.ctype-lower.php,如果b)只需使用strtolower ...

标签: php codeigniter


【解决方案1】:

您可以为 CodeIgniter 提供表单验证的 php 原生函数。这是你的代码应该是这样的

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

【讨论】:

  • 您好,感谢您的帮助!我想我错过了这一行“您也可以使用任何允许一个参数的原生 PHP 函数,例如 trim、htmlspecialchars、urldecode 等。”在文档上。话虽这么说,在我设置规则strtolower之后,如果我使用$this->input->post('username'),所有的都会以小写形式返回?
  • 是的!在您访问$this->input->post('username') 时设置规则后,它应该会为您提供过滤后的值。
【解决方案2】:

callback_ 添加到规则中。

$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|callback_convert_lowercase');

并且回调函数应该返回一些值。

public function convert_lowercase() {
  return strtolower($this->input->post('username'));
}

【讨论】:

  • 谢谢。我知道我需要在 convert_lowercase 函数中返回一些东西,只是我一直想不出该怎么做。
  • 如果您的回调返回布尔值 TRUE/FALSE 以外的任何值,则假定数据是您新处理的表单数据。检查documentation
【解决方案3】:

我会尽力解释!您是否正确设置了数据库配置文件?您是否正确设置了数据库?在你这样做之前确保一切都很好..

这里有一些事情发生了

 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 文档,这太棒了

【讨论】:

  • 感谢您插入数据库查询!
  • 没问题,它会自动为你小写你的结果,所以即使用户输入 UsEr,它也会以用户身份放入数据库
【解决方案4】:

关于 Codeigniter 4,它不再可能在验证期间修改数据。

他们在官方文档中添加了这些信息: “您还可以使用任何返回布尔值并允许至少一个参数,即要验证的字段数据的任何原生 PHP 函数。验证库永远不会更改要验证的数据。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2022-12-04
    • 1970-01-01
    • 2018-08-29
    • 2013-02-04
    • 1970-01-01
    相关资源
    最近更新 更多