» home

symfonyチュートリアル実践【第6日目】

6日目

セキュリティとフォームの値チェック(validation)。
この部分のコードを毎回自分で書くのが面倒だからこそ、フレームワークを使いたい、と自分は思ったわけです。

さて、まずは”Validation file”の指示に従いYAMLでapps/frontend/modules/user/validate/login.ymlにルールを書いていく。パスワードは5文字以上、というルールにsfStringValidatorというクラスを使っていますが、how to validate a formを見るとsfEmailValidatorとか正規表現が使えるsfRegexValidatorなどおよそ必要になりそうなクラスは揃っています。

デフォルトではここで書いたルールに反した入力があるとapps/frontend/modules/user/templates/loginError.phpが呼び出されるのですが、普通はログインのフォームにエラーメッセージを表示したいのでapps/frontend/modules/user/actions/actions.class.phpにhandleErrorLoginメソッドを書き加えます(”Error handling”)。

public function handleErrorLogin()
{
  return sfView::SUCCESS;
}

return sfView::ERROR;にすればloginError.phpになる。

“Template error helpers”でapps/frontend/modules/user/templates/loginSuccess.phpの<div class="form-row">の部分を書き換えてエラーメッセージを表示できるようにする。

“Style errors”でweb/main.cssを書き換える、とあるが僕の環境ではweb/css/main.css。

“Authenticate a user”の項に入り、”Custom validator”で先ほどのlogin.ymlに修正を加える。5日目でapps/frontend/modules/user/actions/actions.class.phpのexecuteLoginで「nickname exists?」とコメントが入っている辺りにvalidation的な処理を入れたがそれをactionのクラスからcustom validatorに移動しよう、という修正。

定義したmyLoginValidatorを書く前に”Password storage”でデータベースに変更を加えておく。

$ symfony propel-build-model
$ symfony propel-build-sql
$ mysql askeet < data/sql/schema.sql

“Password storage”と”Add password in the test data”は書かれている通りに実行。load_data.phpを実行する際にはsetPassword()もちゃんと呼ばれるようになっている。

“Custom validator”でいよいよmyLoginValidatorクラスを書く。保存するファイルはどこでもいいようだがチュートリアル通り、apps/frontend/lib/myLoginValidator.class.phpにした。

“Remove the code from the action”でexecuteLoginを書き換え。

さてここでブラウザを使ってログインしてみようとしたところ、、

[sfAutoloadException]
Autoloading of class "myLoginValidator" failed. Try to clear the symfony cache and refresh. [err0003]

エラー。

キャッシュのクリアってこれかな?

$ symfony cc

実行してブラウザをリロードしたらうまくいった。

“Restrict access”でapps/frontend/modules/question/config/security.ymlに書き込む内容を

add:
  is_secure:   on
  credentials: subscriber

all:
  is_secure:   off

とチュートリアルには書いているが、addではなくてcreateだと思う。ログインしていない状態で/question/createにアクセスすると「You must enter you credential to access this page.」が出るようになる。

続く”How about a bit of refactoring?”のapps/frontend/lib/myUser.phpへの修正もmyUser.class.phpが僕の環境下でのファイル名になっている。バージョンの違いによる差異なのか?一応、myUser.class.phpを編集して動いた。

6日目は比較的スムーズに終了。ふぅ。
しかし結びの台詞がなんとも。。

Don’t forget that the program of the 21st day is still up to you.

先は長いぜ、小僧!と言われているような気がする。

By: funaki | 2006年11月5日 | いじる | Trackback | Comments [RSS 2.0]

コメントはまだありません。 »

RSS feed for comments on this post. TrackBack URI

コメントはお気軽にどうぞ

↑top