1

直接取自CodeIgniter 页面

$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');

这部分:

'trim|required|min_length[5]|max_length[12]|xss_clean'

PHP 是否具有通过 | 分隔值的内置功能?检查,还是 CodeIgniter 手动进行?

如果是这样,

他们为什么不使用这样的东西?

set_rules('username', 'Username', array('trim', 'required' ...));

处理数组而不是浪费不必要的代码来检查不是更容易吗?符号和单独的标签?

4

1 回答 1

3

CodeIgniter 将explode()在该字符串上运行一个,使用|(pipe) 作为分隔符。归根结底,这只是设计师的喜好和创造力。

下面是来自CI 源的片段,它进行了拆分。

// Cycle through the rules for each field, match the
// corresponding $_POST item and test for errors
    foreach ($this->_field_data as $field => $row)
    {
        // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array.
        // Depending on whether the field name is an array or a string will determine where we get it from.
        if ($row['is_array'] === TRUE)
        {
            $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
        }
        elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
        {
            $this->_field_data[$field]['postdata'] = $validation_array[$field];
        }

        // Don't try to validate if we have no rules set
        if (empty($row['rules']))
        {
            continue;
        }

        $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
    }
于 2012-09-07T23:53:39.750 回答