Kohana 表单验证Validation官方教程写的有点粗糙,我也是看了半天才明白。本教程是连载教程,数据还是上一讲的数据结构(
)
第一步:在application/model/article.php中书写验证规则:
//验证规则 public function rules() { return array( 'cid' => array( array('not_empty') ), 'title' => array( array('not_empty'), ), 'content' => array( array('not_empty'), array('min_length', array(':value', 4)), array('max_length', array(':value', 40)), ), ); }
*/ return array( 'cid' => array( 'not_empty' => '文章分类不能为空' ), 'title' => array( 'not_empty' => '文章标题不能为空' ), 'content' => array( 'not_empty' => '文章内容不能为空', 'min_length' => '内容至少4个字符', 'max_length' => '内容不能超过40个字符' ) );
PHP点点通-http://www.phpddt.com
find_all(); $this->template->category = $category; if($_POST) { $artile = ORM::factory('article')->values($_POST); $vali = Validation::factory($_POST, '_external', array()); try{ $artile->save($vali); } catch (ORM_Validation_Exception $e) { $this->template->errors = $e->errors('models'); } } }}