直接来自CodeIgniter page。
$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' ...));
处理数组不是更容易,而是浪费不必要的代码来检查|符号和单独的标签?
答案 0 :(得分:3)
CodeIgniter将在该字符串上运行explode()
,使用|
(管道)作为分隔符。最重要的是设计师的喜好和创造力。
以下是来自CI source的片段,用于进行拆分。
// 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']);
}