Codeigniter使用验证回调来更改提交的值

Cyb*_*kie 3 php forms codeigniter callback

我有一个用户提交CSV的文本输入,例如 red, blue, red, yellow

如果用户提交重复值,例如red如上所示,我想删除副本.我开始回调,但我不知道如何完成它.

    //callback rule
    function _remove_dublicate($str) 
    {
            $val = strtolower($str); //make everything lowercase
            $colors = str_getcsv($val); //create array                          
            $result = array_unique($colors); //remove duplicates

    }
Run Code Online (Sandbox Code Playgroud)

如果有重复项,我该怎么做才能将新字符串提交$result到数据库?

以下是我的表单验证

$this->form_validation->set_rules('colors', 'Colors', 'trim|required|xss_clean|regex_match[/^[a-z, ]+$/i]|callback__remove_dublicate');


        if ($this->form_validation->run() == FALSE) //if validation rules fail
        {           
            $this->load->view('edit/form');
        }
        else //success
        {
            $data = array (
                'colors' => $this->input->post('colors')
            );          
            $this->My_model->colors_update($data);          
        }
Run Code Online (Sandbox Code Playgroud)

编辑

根据Cabaret的建议,我在else删除共和党的声明中加入了这一建议

$colors = str_getcsv($this->input->post('colors')); //create array
$result = array_unique($colors); //remove duplicates
$comma_separated = implode(",", $result); //add back CSV string

$data = array (
    'colors' => $comma_separated
);          
Run Code Online (Sandbox Code Playgroud)

它似乎工作

Wes*_*rch 7

尽管有这些评论,但这是使用回调的完全正当理由,类似于实际更改提交输入值的"准备"规则(例如:trim,xss_clean,strtolower).

你在正确的轨道上,所有你需要做的就是return $result回调,它会改变输入,但要确保你返回一个字符串.例:

//callback rule
function _remove_duplicate($str = '') 
{
    $val = strtolower($str); //make everything lowercase
    $colors = str_getcsv($val); //create array 
    // you could also use explode(',', $colors)
    // if you aren't expecting other delimiters than a comma
    // i.e. no commas within quotes                         
    $result = array_unique($colors); //remove duplicates
    $result = implode(',', $result); // back to string value
    return $result; // Return the value to alter the input
}
Run Code Online (Sandbox Code Playgroud)

现在,如果你想警告用户有重复项而不是简单地删除它们,只要return FALSE找到任何一个,可能是count($colors) === count($result)while $result仍然是一个数组.这取决于你,但只是你知道返回true/false 改变输入的选项是可用的.

您实际上可以将这样的内容写入Form_validation类本身内的新表单验证规则(扩展库,甚至只是一个全局函数),而不需要使用回调,它似乎是一个非常可重用的函数.

这被视为"准备"规则.从用户指南:

除了我们上面使用的验证功能之外,您还可以通过各种方式准备数据.例如,您可以设置如下规则:

$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我们"修剪"字段,将密码转换为MD5,并通过"xss_clean"函数运行用户名,该函数删除恶意数据.

任何接受一个参数的本机PHP函数都可以用作规则,如htmlspecialchars,trim,MD5等.

注意:您通常希望在验证规则之后使用预备功能,因此如果出现错误,原始数据将显示在表单中.

因此,在检查输入有效后,您可以继续清理它,修剪,从逗号分隔的字符串中删除重复项,或任何您想要的内容.任何全局可用的函数都是有效的(包括非本机PHP函数,例如:CI的任何辅助函数,只要它们被加载和定义.),以及属于Form_validation库或其任何扩展的任何函数.你可能有.

需要注意的一件事是:如果函数不存在或不可用,它将被忽略.始终进行测试以确保获得正确的结果.

希望这可以解决一些困惑.