首页 文章

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

提问于
浏览
-2

我正忙着我的第一个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-> data ['message'];

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

1 回答

  • 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'];
    }
    

相关问题