Archive for 11月, 2007
CakePHP1.2でValidation関係を触ってみる。
11月 29th
本当に1.2系列のKnowledge無いんですね・・・・。英語のドキュメントすらないとは・・・。
ってことで、次は1.2系を入れた理由の1つ、入力文字の判定周りを触ってみます。
まずは、Modelで定義する方法。
var $validate = array( 'owner_mail' => array(
'rule' => array('email'),
'message' => 'メールアドレスは正しく入力してください'));
$validateで設定をします。
公式はこんな感じ?
$validate(‘カラム(キー)値’ => array(‘rule’ => array(‘ルール定義’),’message’ => ‘エラー時のメッセージ’));
エラー時のメッセージは入力フォームの直後に出ます。
次にController。
$this -> Model名 -> validates($this ->data);
今回は、データ投入をするのではなくあくまで入力内容チェックなので、これだけ。
ポイントは「validates」。「s」付きですよ!
これでModelに書いたデータを精査します。
最後はView。
< ?php echo $form->error('save等で使った入力キー値', 'エラー文');?>
通常、エラー文は出るので、これはたとえば入力フォームの一番上に列挙する形で置いておくのがいいかもしれません。
良くあるパターンですね。エラーがあるときだけ文章が出てくる感じ。
で、動いてはいるんだけど・・・なんかアラートが出てる罠。
Warning (512): (Model::validates) Parameter usage is deprecated, set the $data property instead [CORE/cake/libs/model/model.php, line 1745]
何々、えーと・・・「パラメータの書き方は非推奨、$dataの特性?を推奨します。」
ん??
validatesの書き方がおかしいのかな・・・・?もうちょっと調査します。
[tags]CakePHP,Validates,1.2,CakePHP1.2[/tags]
PostgreSQLでカラム名(列名)を取り出す方法
11月 29th
CakePHPでscaffoldの代わりみたいな感じで、自動に挿入されるcreatedやID等を取り除いたヘルパーライクな物を作ってます。
で、開発環境がPostgreSQLなので・・・汎用的にカラム名を取り出す方法が全く分からず。
調べていたら会社の先輩が教えてくれました!
とりあえず、かなり特殊なので・・・メモっておきましょう。
問題のSQL文はこちら。
SELECT
pg_class.relname,
pg_attribute.attname,
pg_attribute.atttypmod,
pg_attribute.attnum,
pg_attribute.attalign,
pg_attribute.attnotnull,
pg_type.typname
FROM
pg_class,
pg_attribute,
pg_type
WHERE
pg_class.oid = pg_attribute.attrelid and
pg_attribute.atttypid = pg_type.oid and
pg_class.relname='テーブル名' and
pg_attribute.attnum > 0
ORDER BY
pg_attribute.attnum;
これで、カラム名を含んだ検索結果を出してくれます。
いやぁ、超マニアックな技過ぎ・・・・。
PostgreSQLを使ってプログラミングされてる方は是非どうぞ。
[tags]PostgreSQL,SQL,カラム名を取り出す[/tags]
CakePHP 1.2系でFormHelperを使う(Radio編)
11月 29th
CakePHP1.2系で追加されたヘルパーのFormHelper(フォームヘルパー)を使った話です。
どうしてもラジオボタンが言う事を聞いてくれないんですよね・・・。
勝手に括弧が付いたり、フィールド名で括られたり。
っていうことで、解析。
ヘルパーのプログラムファイルを覗いてみましょう。
インストールディレクトリ/cake/libs/view/helpers/form.php
このフォルダ内に、全てのヘルパーファイルがあるんで困ったらここを見るといいですよ。
ここでFormヘルパーの部分をチェック。
742行目あたりがそうですね。一部を抜粋。
/**
* Creates a set of radio widgets.
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $options Radio button options array.
* @param array $attributes Array of HTML attributes. Use the 'separator' key to
* define the string in between the radio buttons
* @return string
*/
function radio($fieldName, $options = array(), $attributes = array()) {
$attributes = $this->__initInputField($fieldName, $attributes);
$this->__secure();$legend = false;
if (isset($attributes['legend'])) {
$legend = $attributes['legend'];
unset($attributes['legend']);
} elseif (count($options) > 1) {
$legend = Inflector::humanize($this->field());
} $label = true; if (isset($attributes['label'])) { $label = $attributes['label'];
unset($attributes['label']); } $inbetween = null;
if (isset($attributes['separator'])) { $inbetween = $attributes['separator'];
unset($attributes['separator']);
}if (isset($attributes['value'])) {
$value = $attributes['value'];
} else {
$value = $this->value($fieldName);
}$out = array();
ここの所のFunctionの引数に注目。
function radio($fieldName, $options = array(), $attributes = array())
ふむふむ、ってことは、radioは第3引数までで、第2と第3はArrayで渡せと。
で、該当する中身を読む。
$attributesの配列の中に入れられるものは3つの変数。$legendと$labelと$separator。
この3つのどれかでなるだろ・・・と思っていじったら出来た。
もし、枠囲いされてフィールド名が出て邪魔だと思う場合は・・・
< ?php echo $form->radio(’フィールド名',array('選択候補1' => 'POST値' , '選択候補2' => 'POST値'),array('legend' => false)); ?>
ポイントはlegendという値ですね。
legend = false →枠が表示されない(フィールド名が表示されない)
legend = true →「1」と表示
legend = ‘文字列’ →枠囲いの表題が「文字列」に。
何はともあれ、悩んだ末に出来てよかった・・・。
というかCakePHPの1.2に関する記述がやたら少なくて、見つけ出すのも一苦労ですねぇ・・・。
メモ書き程度にここに色々残していきますが、動かないとか出来ない報告あれば対応します。