我正忙于我的第一个编码"实践"项目。我已经使用了flexi_auth作为验证库,并且非常喜欢他们在模型中而不是在控制器中实现表单验证的方式,从而坚持MVC(Fat模型)原则。
我正试图用相同的逻辑构建另一个表单,但对于我的生命,我似乎无法通过控制器从模型到视图获得验证错误设置的错误消息。请注意,我正在使用布局帮助器,因此对视图的异常调用。
任何帮助将不胜感激。下面是我的代码:
控制器:
//Start Information Desk Query()
public function information()
{
if ($this->input->post(‘informationForm_submit’))
{
$this->load->model(‘contact_model’);
$this->contact_model->information_post();
// information_post validation failed
}
$this->data[‘title’] = ‘Information Desk’;
$this->layout->show(‘contact/information_view’, $this->data);
}
//End Information Desk Query()
模型:
//Start Information Request()
public function information_post()
{
$validation_rules = array(
array(‘field’ => ‘informationEmail’, ‘label’ => ‘Email’, ‘rules’ => ‘required|valid_email’),
array(‘field’ => ‘informationQuery’, ‘label’ => ‘Message’, ‘rules’ => ‘required|max_length[500]’)
);
$this->form_validation->set_rules($validation_rules);
if ($this->form_validation->run())
{
return TRUE;
}
else
{
$this->data[‘message’] = validation_errors(’<li class=“error_msg”>’, ‘</li>’);
return FALSE;
}
}
//End Information Request()
视图:
<?php
if (! empty($message))
{
echo ‘<div class = “error_message”>
<div class =“error_header”>Please correct the following</div>
<div class = “error_image my-icons-Actions-window-close-icon”></div>
<ul class = “error_text”>’.$message.’</ul></div>’;
}
?>
**请注意,表单验证在此代码中是有效的。如果我输入以下内容:echo $ this ->数据("信息"),
…在模型中,然后错误显示,但显然不是作为我的视图文件的一部分,但在它之前。PS.请放轻松,如果这是一个愚蠢的问题,但我是一个新手编码,实际上MVC。
你正在设置模型的属性而没有在控制器中获取它
你应该在你的控制器中这样做:
if ($this->input->post('informationForm_submit'))
{
$this->load->model('contact_model');
$this->contact_model->information_post();
// required addition
$this->data['message'] = $this->contact_model->data['message'];
}