Kohana 2.2 の Validation クラスの使い方
基本的な使い方。
<?php defined('SYSPATH') or die('No direct script access.'); class Test_Controller extends Controller { public function __construct() { parent::__construct(); } public function index() { $_POST = null; // 疑似 $_POST データを生成する $_POST = array( 'tel' => '090-0300-0000', 'url' => 'http://www.google.co.jp/', 'price' => '1000', 'name' => ' 名無し太郎'); $post = new Validation($_POST); // $_POST['name'] で trim 関数を使ってスペースを削除する $post->pre_filter('trim', 'name'); $post->add_rules('tel', 'required', 'alpha_dash'); // URL 形式のチェックが甘い // callback を利用して確認をしたほうがよい $post->add_rules('url', 'required', 'url'); // digit のため、負数の場合はエラーが発生する $post->add_rules('price', 'required', 'digit'); $post->add_rules('name', 'required', 'standard_text', 'length[1,6]'); if ($post->validate()) { echo 'good'; } else { echo '不正なデータが投稿されました。'; $errors = $post->errors(); foreach ($errors as $key => $value) { echo $key . ' は ' . $value . 'でエラーが起きました。'; } } } } ?>