gpt4 book ai didi

php - 验证不适用于 HMVC Codeigniter

转载 作者:可可西里 更新时间:2023-11-01 08:45:57 24 4
gpt4 key购买 nike

我是 HMVC Codeigniter 的新手。我会使用 codeigniter 对 codeigniter 的 HMVC 格式进行表单验证,但它没有显示任何效果,这意味着 formvalidation 不适用于我的项目。但是这段代码应该适用于 MVC codeigniter。所以请帮助我在我的项目中解决这个问题。我很感激谁帮助我解决了我的项目中的这个问题。

** 我有一个如下所示的关联 Controller 文件 feedback.php**

 function index($offset=0){
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');

$this->form_validation->set_rules('name','Name','trim|required');

$this->form_validation->set_rules('email','Email Address','trim|valid_email|required');
$this->form_validation->set_rules('message','Message','trim|required');

if($this->form_validation->run()){

$data1=array(
'name' => $this->input->post("name"),

'email' => $this->input->post("email"),

'message' => $this->input->post("message"),

);


}

}
$data=array('body1'=>'feedback');
$this->load->view('temp',$data);

}

我有一个关联 View 文件 feedback.php,如下所示

<form action="<?php echo site_url()?>"  name="FeedbackForm" method="post">

<span style="color:#F00">

<?php echo validation_errors(); ?>

</span>
<table>

<tr>
<td><label>Name</label></td>
<td><input id="name" name="name" type="text" /></td>
</tr>

<tr>
<td><label>Email</label></td>
<td><input type="email" name="email" id="email" /></td>
</tr>

<tr>
<td><label>Message</label></td>
<td><textarea name="message" rows="2" cols="16" ></textarea></td>
</tr>

<tr>
<td></td>
<td><input type="submit" id="submit" value="Send" /> </td>
</tr>

</table>

</form>

最佳答案

在某些使用 HMVC 的情况下,您可能需要使用 MY_Form_Validation 库。如果在 HMVC 的表单验证中也使用回调将不起作用,除非有下面的代码。

如果学习最好使用 codeigniter 2.2.1 版本,codeigniter 3 出来了,但仍然很少有错误。

另一件需要注意的事情是你可能需要在 config/routes.php 中配置你的路由

$route['feedback'] =  "module-folder-name/feedback/index";
$route['feedback/updates/(:any)'] = "module-folder-name/feedback/updates/$1";
$route['feedback/add'] = "module-folder-name/feedback/add";
$route['feedback/delete'] = "module-folder-name/feedback/delete";

在表单上将站点 url 更改为 base_url() base_url 以及在 routes.php 中设置的 Controller 名称

在你的表格上

<?php echo base_url('feedback')?>

另外,如果需要从 url 获取 id,为什么需要 $offset=0 查看 uri 段。

<?php

class MY_Form_validation extends CI_Form_validation {

function run($module = '', $group = '') {
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}

}

然后在 Controller 中将是 run($this)

class Feedback extends MX_Controller {

public function index() {

$this->load->helper('url'); // Try autoloading it
$this->load->helper('form'); // Try autoloading it
$this->load->library('form_validation');

$this->form_validation->set_rules('name','Name','trim|required');

$this->form_validation->set_rules('email','Email Address','trim|valid_email|required');
$this->form_validation->set_rules('message','Message','trim|required');

if ($this->form_validation->run($this) == FALSE) {

// Load Main View & Data.

$this->load->view('folder/feedback');

} else {

// Load Success View Or Redirect

$this->load->model('module-name/model_feedback');

$this->model_feedback->update();

// Or

$this->model_feedback->insert();

redirect('controller-name');

}

}

}

模型

public function update() {
$data = array(
'username' => $this->input->post('email'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);

$this->db->where('your_id', $your_id); // May be uri segment() etc read userguide
$this->db->update('tablename', $data);
}

public function insert() {
$data = array(
'username' => $this->input->post('email'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);

$this->db->insert('tablename', $data);
}

查看

<form action="<?php echo base_url('feedback')?>"  name="FeedbackForm" method="post">

<?php echo validation_errors(); ?>

<table>
<tr>
<td><label>Name</label></td>
<td><input id="name" name="name" type="text" /></td>
</tr>

<tr>
<td><label>Email</label></td>
<td><input type="email" name="email" id="email" /></td>
</tr>

<tr>
<td><label>Message</label></td>
<td><textarea name="message" rows="2" cols="16" ></textarea></td>
</tr>

<tr>
<td></td>
<td><input type="submit" id="submit" value="Send" /> </td>
</tr>


</form>

Codeigniter 论坛:http://forum.codeigniter.com/Codeigniter 2.2.1 用户指南 http://www.codeigniter.com/user_guide/

关于php - 验证不适用于 HMVC Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28923840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com