Codeigniter:更新记录时的表单验证
我很难让它发挥作用。也许我的逻辑全错了,但我觉得有经验的人可以帮我 我有一个页面“”,用户可以在其中编辑其帐户信息。表单提交时,会触发“设置\保存”方法。如果表单提交成功,则一切正常。但是,如果其中一个字段未验证,则URL将保持在“我确实希望它保持打开状态” account.php控制器Codeigniter:更新记录时的表单验证,codeigniter,Codeigniter,我很难让它发挥作用。也许我的逻辑全错了,但我觉得有经验的人可以帮我 我有一个页面“”,用户可以在其中编辑其帐户信息。表单提交时,会触发“设置\保存”方法。如果表单提交成功,则一切正常。但是,如果其中一个字段未验证,则URL将保持在“我确实希望它保持打开状态” account.php控制器 function settings() { $data['records'] = $this->account_model->getAccountSettings("sam"); $
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\u settings\u view.phpview
我有以下几行:
<?=form_open('account/settings_save');?>
您只需要一个控制器功能,如下所示:
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);
}
然后在视图中:
如果表单中有多余的空间,则可能会导致表单验证失败。请检查以下内容,确保没有该错误
$this->form_validation->set_rules('sub_service ','Sub ServiceName','trim|required');
$this->form_validation->set_rules('sub_service','Sub Service Name','trim|required');
必须没有空格。看起来我有进展了。如果验证没有通过呢?我想我需要一个“else”在那里。如果是这样,我将如何处理加载视图的最后两行代码?你不需要else。如果验证失败,将加载同一个视图,并返回一个字符串(或多个),并重新填充表单。如果成功,您的信息将添加到您的数据库中,并显示成功消息。谢谢,我的代码中有一个错误阻止了这一点。这太棒了,终于让它工作了。谢谢!