dscqrkvr9562034621 2013-06-01 08:07
浏览 61
已采纳

Codeigniter,Fat模型和瘦控制器[关闭]

I’m busy with my first codeigniter “practice” project. I’ve used flexi_auth as a validation library and really liked the way that they implement the form validation in the model and not in the controller, thus sticking to MVC(Fat models) principal.

I’m trying to build another one of my forms with the same logic but for the life of me I can’t seem to get the error message set by validation errors from the model via the controller to the view. Please note I’m using a layout helper, thus the abnormal call to view.

Any help will be appreciated. Here is my code:

Controller:

//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()

Model:

//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()

View:

<?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>’;
      }
  ?>

**Please note that the form validation is functional in this code. If I put the following: echo $this->data[‘message’];

....in the model then the errors are displayed but obviously not as a part of my view file but before it. PS. please take it easy if this is a stupid question but I am a newbie to codeigniter and actually to MVC.

  • 写回答

1条回答 默认 最新

  • douli8040 2013-06-01 08:30
    关注

    You are setting property of model and not getting it in controller

    you should do something like this in your controller:

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值