我是 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/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下