|
|
| 1行目: |
1行目: |
| ==サンプル==
| | [[php/fuelphp/validation/基本]] |
| $val = Validation::forge('my_validate');
| |
| $val->add_field('app_name', 'AppName', 'required|max_length[63]'); // 必須で63文字まで
| |
| $val->add_field('description', 'Description', 'max_length[255]'); // 任意で255文字まで
| |
| $val->add_field('test_id', 'TestId', 'required|valid_string[numeric]'); // 必須で数字
| |
| $val->add_field('month', 'Month', 'required|valid_string[numeric]|numeric_min[1]|numeric_max[31]'); // 必須で1~31まで
| |
| $val->add_field('month', 'Month', 'match_pattern[/[\w]+/]'); // 正規表現で文字のみ
| |
| $val->add_field('ymd', 'Ymd', 'match_pattern[/[12][\d]{3}[01][\d][0123][\d]/]'); // 簡易yyyymmdd判定
| |
| if ($val->run())
| |
| // 正常
| |
| } else {
| |
| // 異常
| |
| Session::set_flash('error', $val->error());
| |
| }
| |
| ==エラー表示==
| |
| foreach ($val->error() as $error) {
| |
| echo $error;
| |
| }
| |
| | |
| ==コントローラーにValidate記述==
| |
| public function action_index()
| |
| {
| |
| return Response::forge(View::forge('user/index'));
| |
| }
| |
| public function post_index()
| |
| {
| |
| $val = Validation::forge('user_validation');
| |
| $val->add_field('id', 'Id', 'required');
| |
| if ($val->run()) {
| |
| $user = Model_User::forge(array(
| |
| 'id' => Input::get('id'),
| |
| 'name' => Input::get('name'),
| |
| ));
| |
| if ($user and $user->save()) {
| |
| Session::set_flash('success', e('Added user #'.$user->id.'.'));
| |
| Response::redirect('user');
| |
| } else {
| |
| Session::set_flash('error', e('Could not save user.'));
| |
| }
| |
| } else {
| |
| Session::set_flash('error', $val->error());
| |
| }
| |
| return Response::forge(View::forge('user/index'));
| |
| }
| |
| ==公式サイト==
| |
| http://fuelphp.com/docs/classes/validation/validation.html
| |