用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3005|回复: 14
收起左侧

[已解决] 新人求救,卡在手册教程$this->form_validation->run()一直为false

[复制链接]
发表于 2016-9-8 12:10:14 | 显示全部楼层 |阅读模式
本帖最后由 萌萌哒CI新手 于 2016-9-13 10:07 编辑

http://codeigniter.org.cn/user_g ... ate_news_items.html在这个表单添加条目教程中 每一步都按照上面做了 代码也是复制过去的  success也自己创建了  但是点击submit后数据库不会insert 也不会跳转到success页面  应该是$this->form_validation->run()没有改变···
求帮助  卡在这好久了  我的 base_url设置为localhost:8001------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
折腾了半天 终于找到问题了   form_open('new/create');函数转化成html后 代码如下
<form action="localhost:8001/index.php/news/create" method="post" accept-charset="utf-8">
但是当action输入为空时 已经能够跳转至localhost:8001/index.php/news 即当前网页的根目录下  

记得将config中base_url的值改为'http://localhost:端口号' http://加网址即绝对地址...
发表于 2016-9-8 18:14:40 | 显示全部楼层
萌萌哒CI新手 发表于 2016-9-8 13:40
感觉这个框架不太智能   代码自动补全也没有  还是从github自己下载的插件  load文件也不能快捷跳到文件位 ...

CI 这个锅背的太冤枉。。。。 IDE 太弱没法识别 CI 的代码,这锅不能让 CI 背啊,既然是符合 PHP 语法的东西,为什么 IDE 识别不了?。。。
 楼主| 发表于 2016-9-8 13:24:15 | 显示全部楼层
本帖最后由 萌萌哒CI新手 于 2016-9-8 13:29 编辑

controller/News.php`
----------------------------------------------
<?php
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }
    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->view('news/success');
        }
    }
}

views/news/create.php
----------------------------------------------
<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

<label for="title">Title</label>
<input type="input" name="title" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />

<input type="submit" name="submit" value="Create news item" />

</form>

model/New_model.php
----------------------------------------------
<?php
class News_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }
    public function set_news()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
    }
}


 楼主| 发表于 2016-9-9 09:26:14 | 显示全部楼层
Hex 发表于 2016-9-8 18:14
CI 这个锅背的太冤枉。。。。 IDE 太弱没法识别 CI 的代码,这锅不能让 CI 背啊,既然是符合 PHP 语法的 ...

IDE是phpstorm  之前用phalcon的时候  phalcon官方提供phalcon devtool composer之后就有自动补全的外部库了,但是CodeIgniter官方网站并没有看到类似的文件和外部库提供下载,我也是后来去github找到大神提供的auto_helper库才实现了代码补全功能,所以觉得codeigniter不太人性化  也许是官方提供了库我没找到吧。至于您说的IDE太弱 我认为phpstorm应该是比较主流的php开发IDE了 应该不存在太弱的问题
 楼主| 发表于 2016-9-8 12:48:50 | 显示全部楼层
还有一个很骚的问题就是我生成的超链接在页面中点击就不能连接过去  但是在源码中查看再点击就可以连接过去  新窗口中打开也可以连接过去
发表于 2016-9-8 13:09:56 | 显示全部楼层
要贴代码才知道呢
 楼主| 发表于 2016-9-8 13:25:30 | 显示全部楼层
views/news/success.php

<?php

    echo "congratulations";
 楼主| 发表于 2016-9-8 13:31:05 | 显示全部楼层
view('news/create'); 是可以执行的  我输入http://localhost:8001/index.php/news/create有表单页面  但是submit没反应  
 楼主| 发表于 2016-9-8 13:36:36 | 显示全部楼层
萌萌哒CI新手 发表于 2016-9-8 13:24
controller/News.php`
----------------------------------------------

还有个很重要的问题 我在输入$this->news_model->set_news(); 后  IDE显示 new_model not found in News这是本来就会这样还是我代码出问题了? 我在构造函数中明明输入了$this->load->model('news_model');
是我自己的问题还是IDE的问题还是CI的问题···  感觉特别不人性化  好像我输入$this->load->model('news_model');  为什么不能够从这行代码跳转到 news_model.php呢
 楼主| 发表于 2016-9-8 13:40:37 | 显示全部楼层
感觉这个框架不太智能   代码自动补全也没有  还是从github自己下载的插件  load文件也不能快捷跳到文件位置
发表于 2016-9-8 16:48:06 | 显示全部楼层
我没记错的话  $this->load->helper('url_helper'); 应该是  $this->load->helper('url');
发表于 2016-9-8 16:49:50 | 显示全部楼层
CI3 毕竟不支持namespace 对于框架内部的类函数只要是通过load加载的 都没办法给你自动提示 不过要实现自动提示还是可以的  你可以自己命名一个属性 通过注释ide会自动提示 ci4后面加入了namespace应该就好多了

本版积分规则