CodeIgniter从表单字段中获取值

时间:2014-06-16 14:54:13

标签: php codeigniter

我知道这是一个愚蠢的问题,但在搞乱之后,我感到困惑的是正确的做法。

以下是我的代码:

查看registration_form.php:

$input_data = array(
    'name'      => 'user_name',
    'id'        => 'user_name',
    'value'     => set_value('user_name'),
    'maxlength' => MAX_CHARS_4_USERNAME
);

Controller register.php:

$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );

表格验证配置:

$config['customer_creation_rules'] = array(
    array(
        'field' => 'user_name',
        'label' => 'USERNAME',
        'rules' => 'trim|required|alpha|strtolower'
    )
);

模型user_model.php:

public function create_user( $role, $insert_array = array() )
    {
        // The form validation class doesn't allow for multiple config files, so we do it the old fashion way
        $this->config->load( 'form_validation/administration/create_user/create_' . $role, TRUE );
        $this->validation_rules = config_item( $role . '_creation_rules' );


        $form_username = $this->config->item($role . '_creation_rules', 'field');


        echo $form_username;
    }

我现在要做的是检查用户输入的用户名,并在插入数据库之前自动为输入用户名添加数字。

最初我想过从表单验证中获取输入的用户名,在搞乱之后,无论我尝试过什么,我都无法获得价值。

我做错了吗?我只是从$ _POST获得了吗?

希望你们能帮助我解决这个问题。提前谢谢!

1 个答案:

答案 0 :(得分:0)

这会显示完整列表(底部为waaaay):http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

我没有在那里看到那个,但是它说你可以使用任何只需要一个参数的PHP函数,这样才能工作,但是你必须深入研究文档以找出原因它不是。

我认为你不能为表单设置规则,你必须按照变量设置它们,如文档所示:

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