Codeigniter:更新记录时的表单验证

时间:2010-01-09 16:29:21

标签: codeigniter

我很难让这个工作。也许我的逻辑是错的,但我认为有经验的人可以帮助我。

我有一个页面“http://domain.com/account/settings”,用户可以在其中修改自己的帐户信息。表单提交时,它会触发“settings_save”方法。如果表单成功提交,则一切正常,但是,如果其中一个字段未验证,则网址仍为“http://domain.com/account/settings_save”我实际上希望它保持在http://domain.com/account/settings

account.php 控制器

function settings() {
    $data['records'] = $this->account_model->getAccountSettings("sam");
    $this->load->view('account_settings_view', $data);
}

function settings_save() {
    $this->load->library('validation');

    $records['email']   = "trim|required|min_length[4]|xss_clean";
    $records['gender']  = "trim|required|xss_clean";
    $records['seeking'] = "trim|required|xss_clean";
    $records['marital_status']  = "trim|required|xss_clean";
    $records['kids']    = "trim|required|xss_clean";
    $records['drinking']    = "trim|required|xss_clean";
    $records['smoking'] = "trim|required|xss_clean";
    $records['ethnicity']   = "trim|required|xss_clean";
    $records['body_type']   = "trim|required|xss_clean";
    $records['zipcode'] = "trim|required|min_length[5]|numeric|xss_clean";

    $this->validation->set_rules($records);

    if ($this->validation->run() == false)
    {
        $this->load->view('account_settings_view', $records);                   
    }

    else
    {
        $records = array();
        $records['email'] = $this->validation->email;
        $records['gender'] = $this->validation->gender;
        $records['seeking'] = $this->validation->seeking;
        $records['marital_status'] = $this->validation->marital_status;
        $records['kids'] = $this->validation->kids;
        $records['drinking'] = $this->validation->drinking;                     
        $records['smoking'] = $this->validation->smoking;
        $records['ethnicity'] = $this->validation->ethnicity;
        $records['body_type'] = $this->validation->body_type;
        $records['zipcode'] = $this->validation->zipcode;       

        $this->account_model->saveAccountSettings("sam", $records);
        $this->session->set_flashdata('message', 'Done. You have added new task.');            

        redirect('account/settings');
        //redirect('account/settings');
    }
}

account_settings_view.php 查看

我有以下一行:

<?=form_open('account/settings_save');?>

2 个答案:

答案 0 :(得分:1)

你只需要一个控制器功能,如下所示:

function settings() {

    $this->load->library('validation');
    $records['email']   = "trim|required|min_length[4]|xss_clean";
    $records['gender']  = "trim|required|xss_clean";
    $records['seeking'] = "trim|required|xss_clean";
    $records['marital_status']  = "trim|required|xss_clean";
    $records['kids']    = "trim|required|xss_clean";
    $records['drinking']    = "trim|required|xss_clean";
    $records['smoking'] = "trim|required|xss_clean";
    $records['ethnicity']   = "trim|required|xss_clean";
    $records['body_type']   = "trim|required|xss_clean";
    $records['zipcode'] = "trim|required|min_length[5]|numeric|xss_clean";

    $this->validation->set_rules($records);

    if ($this->form_validation->run() == TRUE) {
        $records = array();
        $records['email'] = $this->validation->email;
        $records['gender'] = $this->validation->gender;
        $records['seeking'] = $this->validation->seeking;
        $records['marital_status'] = $this->validation->marital_status;
        $records['kids'] = $this->validation->kids;
        $records['drinking'] = $this->validation->drinking;                       
        $records['smoking'] = $this->validation->smoking;
        $records['ethnicity'] = $this->validation->ethnicity;
        $records['body_type'] = $this->validation->body_type;
        $records['zipcode'] = $this->validation->zipcode;     

        $this->account_model->saveAccountSettings("sam", $records);
        $this->session->set_flashdata('message', 'Done. You have added new task.');            

    } 

    $data['records'] = $this->account_model->getAccountSettings("sam");
    $this->load->view('account_settings_view', $data); 
}

然后在视图中:

<form method="post" action=''>

这将发布到自己。

答案 1 :(得分:-2)

如果存在任何额外空间,则有可能无法通过表单验证。检查以下内容,确保没有错误。

$this->form_validation->set_rules('sub_service ','Sub ServiceName','trim|required');
$this->form_validation->set_rules('sub_service','Sub Service Name','trim|required');

必须没有空格。