Codeigniter,Fat模型和瘦调控制​​器

时间:2013-06-01 08:07:28

标签: php codeigniter

我正忙着我的第一个codeigniter“练习”项目。我已经使用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-&gt; data ['message'];

....然后在模型中显示错误,但显然不是我的视图文件的一部分,而是在它之前。 PS。如果这是一个愚蠢的问题,请放轻松,但我是codeigniter的新手,实际上是MVC。

1 个答案:

答案 0 :(得分:1)

您正在设置模型的属性而不是在控制器中获取

你应该在你的控制器中做这样的事情:

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'];
}
相关问题