https://github.com/liu21st/examples/blob/master/Examples/form/
经过Debug发现:
// 处理表单数据
public function insert() {
$Form = D("Form");//这行代码初始化的是Think\Model,而不是FormModel类。
if ($Form->create()) {
if (false !== $Form->add()) {
$this->success('数据添加成功!');
} else {
$this->error('数据写入错误');
}
} else {
// 字段验证错误
$this->error($Form->getError());
}
}整个项目代码见附件,麻烦各位帮忙看看。<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
// $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
// echo "hello world.";
$this->display();
}
// 处理表单数据
public function insert() {
// die('==========');
$Form = D("Form");
if ($Form->create()) {
if (false !== $Form->add()) {
$this->success('数据添加成功!');
} else {
$this->error('数据写入错误');
}
} else {
// 字段验证错误
$this->error($Form->getError());
}
}
public function hello(){
echo "hello";
}
// public function listAction(){
// echo "list";
// }
}Model:<?php
namespace Home\Model;
use Think\Model;
class FormModel extends Model {
// 自动验证设置
protected $_validate = array(
array('title', 'require', '标题必须!', 1),//1为必须验证
array('title', '', '标题已经存在', 0, 'unique', self::MODEL_INSERT),
array('content', 'require', '内容必须'),
);
// 自动填充设置
protected $_auto = array(
array('create_time', 'time', self::MODEL_INSERT, 'function'),
);
}
?> 最佳答案