public function action_send() { // CSRF対策 if (!Security::check_token()) { throw new HttpInvalidInputException('ページ遷移が正しくありません'); } $form = $this->forge_form(); $val = $form->validation()->add_callable('MyValidationRules'); if (!$val->run()) { $form->repopulate(); $this->template->title = 'コンタクトフォーム: エラー'; $this->template->content = View::forge('form/index'); $this->template->content->set_safe('html_error', $val->show_errors()); $this->template->content->set_safe('html_form', $form->build('form/confirm')); return; } $post = $val->validated(); $post['ip_address'] = Input::ip(); $post['user_agent'] = Input::user_agent(); unset($post['submit']); // データベースへ保存 $model_form = Model_Form::forge($post); $ret = $model_form->save(); if (!$ret) { Log::error('データベース保存エラー', __METHOD__); $form->repopulate(); $this->template->title = 'コンタクトフォーム: サーバエラー'; $this->template->content = View::forge('form/index'); $html_error = '<p>サーバでエラーが発生しました。</p>'; $this->template->content->set_safe('html_error', $html_error); $this->template->content->set_safe('html_form', $form->build('form/confirm')); return; } // メールの送信 try { $mail = new Model_Mail(); $mail->send($post); $this->template->title = 'コンタクトフォーム: 送信完了'; $this->template->content = View::forge('form/send'); return; } catch (EmailValidationFailedException $e) { Log::error('メール検証エラー: ' . $e->getMessage(), __METHOD__); $html_error = '<p>メールアドレスに誤りがあります。</p>'; } catch (EmailSendingFailedException $e) { Log::error('メール送信エラー: ' . $e->getMessage(), __METHOD__); $html_error = '<p>メールを送信できませんでした。</p>'; } $form->repopulate(); $this->template->title = 'コンタクトフォーム: 送信エラー'; $this->template->content = View::forge('form/index'); $this->template->content->set_safe('html_error', $html_error); $this->template->content->set_safe('html_form', $form->build('form/confirm')); }
public function test_新規データをテーブルに保存する() { $data = array('name' => '藤原義孝', 'email' => '*****@*****.**', 'comment' => '君がため 惜しからざりし 命さえ 長くもがなと 思ひけるかな', 'ip_address' => '10.11.12.13', 'user_agent' => 'Mozilla/2.02 (Macintosh; I; PPC)'); $form = Model_Form::forge($data); // 新規データをデータベースに挿入 $ret = $form->save(); // 挿入されたデータをデータベースから検索 $form = Model_Form::find($form->id); foreach ($data as $field => $value) { $this->assertEquals($value, $form[$field]); } }
public function action_create() { if (Input::method() == 'POST') { $val = Model_Form::validate('create'); if ($val->run()) { $form = Model_Form::forge(array('name' => Input::post('name'), 'email' => Input::post('email'), 'comment' => Input::post('comment'), 'ip_address' => Input::post('ip_address'), 'user_agent' => Input::post('user_agent'))); if ($form and $form->save()) { Session::set_flash('success', e('Added form #' . $form->id . '.')); Response::redirect('admin/form'); } else { Session::set_flash('error', e('Could not save form.')); } } else { Session::set_flash('error', $val->error()); } } $this->template->title = "Forms"; $this->template->content = View::forge('admin/form/create'); }