Codeigniter:更新记录时的表单验证 [英] Codeigniter: Form validation when updating records
问题描述
我有一个艰难的时间让这个工作。也许我的逻辑是错误的,但我认为有更多经验的人可以帮助我。
I'm having a tough time getting this to work. Maybe my logic is all wrong, but I figure someone with more experience can help me.
我有一个页面 http://domain.com/account/settings ,用户可以在其中修改其帐户信息。当表单提交时,它触发settings_save方法。如果表单提交成功,一切都很好,但是,如果字段之一未验证,则该网址仍保留在 http://domain.com/account/settings_save 我其实想让它留在 http ://domain.com/account/settings
I have a page "http://domain.com/account/settings" where users can edit their account information. When the form submits, it triggers the "settings_save" method. All is fine if the form submits successfully, however, in the event one of the field doesn't validate, the URL remains at "http://domain.com/account/settings_save" I actually want it to stay on 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');?>
推荐答案
你只需要一个控制器函数,
You only need one controller function, something like this:
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 =postaction =''>
这篇关于Codeigniter:更新记录时的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!