酔っぱらいスクリプターの備忘録。よく消える(意味ねぇ
Validatorのお話 Yii Framework触るぜ日記(2)
使うよね。
バリデーター。
備忘録しとかないと忘れる(><)
使い方
modelのrulesの配列に突っこむ。
一つの要素に対してvalidatorを指定するんじゃなくて
「コレとコレとコレにこのバリデーションかける」
って感じで対象をカンマ区切りで指定する。
array('name, email', 'required'),
一個目の引数が対象。
二個目がバリデーター。
あれば三つ目以降にオプションを指定する感じ。
何か新鮮。
二個目のバリデーターにどんな指定があるかってゆーと
http://www.yiiframework.com/doc/api/CValidator
ココみれ。
三個目のオプションはそれぞれのC○○Validatorの説明見れば書いてる。
array('login_password', 'match', '/^[a-zA-Z0-9]$/','message'=>'半角英数字にしれ!'),
ってやってエラーメッセージも変えれるよ。
エラーメッセージ
‘language’=>’ja’を/app/config/main.phpの配列に設定したらエラー文言も日本語になる。
ちょっと気持ち悪い文言だったら(例:{attribute} は空白ではいけません。)変えちゃおう。
/yii/framework/messages/ja/yii.phpを好きにしちゃって下さい。
好きにしていいはず。
独自のバリーデーター
/app/extentions/に入れてrulesでapplication.extentions.hogeって指定すればOK.
例)
rulesに
array('name', 'application.extensions.shimiValidator'),
とシナリオをセットする。
/app/protected/extensions/shimiValidator.phpをおく。
shimiValidator.phpの中身は
<?php
class shimiValidator extends CValidator
{
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if( $this->isEmpty($value) ){
if( $this->allowEmpty ){
return;
}
}
if( ! preg_match("/shimizu/",$value) ){
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} が[shimizu]じゃねぇ!!.');
$this->addError($object,$attribute,$message);
}
}
}
こんな感じで。
メンバ関数validateAttribute($obj,$attr){}が必須なんでね。
そんな感じー。
これで安心かな(・д・)
関連する記事はありません。
| Print article | This entry was posted by shimizu on 2010年2月20日 at 8:11 PM, and is filed under Yii Framework. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
