Codeigniter - 使用edit_unique编辑表单(重新填充)

时间:2013-04-10 14:27:04

标签: php codeigniter validation codeigniter-form-helper

似乎edit_unique函数(即here - Validating Uniqueness In CodeIgniter When Updating A Record}会杀死set_value函数。

一切正常,就像这样......

echo form_input('username', set_value('username',$user->username));

但是在使用edit_unique验证时,提交表单后该值为空。后变量是可以的,验证也没有错误 - 但是没有设置值。

知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:6)

好的 - 我自己找到了。如果是真的,则没有返回值。也许任何人都面临同样的问题......使用这个功能,它可以工作:

function edit_unique($value, $params)  {
    $CI =& get_instance();
    $CI->load->database();

    $CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");

    list($table, $field, $current_id) = explode(".", $params);

    $query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();

    if ($query->row() && $query->row()->id != $current_id)
    {
        return FALSE;
    } else {
        return TRUE;
    }
}

答案 1 :(得分:0)

Petra 的回答很有效,如果由于某种原因它对您不起作用,请检查您的主键是否为“id”,如果不是,则需要进行一些更改。

改变

list($table, $field, $current_id) = explode(".", $params);

if ($query->row() && $query->row()->id != $current_id)

list($table, $field, $current_id, $key) = explode(".", $params);

if ($query->row() && $query->row()->$key != $current_id)

然后添加如下规则:

$this->form_validation->set_rules(
    'form_field', 
    'Form Label', 
    'required|trim|edit_unique[table.column.' . $edit . '.unique]'
);

其中 unique 是您的唯一键/主键,而 $edit 是它的值。

相关问题